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

import com.google.crypto.tink.shaded.protobuf.ByteString;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes2.dex */
public final class k extends l {

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

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

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

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

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

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

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

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

    public k(InputStream inputStream) {
        Charset charset = b0.f23882a;
        if (inputStream == null) {
            throw new NullPointerException("input");
        }
        this.f23917e = inputStream;
        this.f23918f = new byte[4096];
        this.f23919g = 0;
        this.f23921i = 0;
        this.f23923k = 0;
    }

    public final byte[] A(int i10) throws IOException {
        byte[] bArrB = B(i10);
        if (bArrB != null) {
            return bArrB;
        }
        int i11 = this.f23921i;
        int i12 = this.f23919g;
        int length = i12 - i11;
        this.f23923k += i12;
        this.f23921i = 0;
        this.f23919g = 0;
        ArrayList<byte[]> arrayListC = C(i10 - length);
        byte[] bArr = new byte[i10];
        System.arraycopy(this.f23918f, i11, bArr, 0, length);
        for (byte[] bArr2 : arrayListC) {
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }

    public final byte[] B(int i10) throws IOException {
        if (i10 == 0) {
            return b0.f23883b;
        }
        if (i10 < 0) {
            throw InvalidProtocolBufferException.f();
        }
        int i11 = this.f23923k;
        int i12 = this.f23921i;
        int i13 = i11 + i12 + i10;
        if (i13 - this.f23930c > 0) {
            throw new InvalidProtocolBufferException("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit. If reading multiple messages, consider resetting the counter between each message using CodedInputStream.resetSizeCounter().");
        }
        int i14 = this.f23924l;
        if (i13 > i14) {
            K((i14 - i11) - i12);
            throw InvalidProtocolBufferException.j();
        }
        int i15 = this.f23919g - i12;
        int i16 = i10 - i15;
        InputStream inputStream = this.f23917e;
        if (i16 >= 4096) {
            try {
                if (i16 > inputStream.available()) {
                    return null;
                }
            } catch (InvalidProtocolBufferException e10) {
                e10.h();
                throw e10;
            }
        }
        byte[] bArr = new byte[i10];
        System.arraycopy(this.f23918f, this.f23921i, bArr, 0, i15);
        this.f23923k += this.f23919g;
        this.f23921i = 0;
        this.f23919g = 0;
        while (i15 < i10) {
            try {
                int i17 = inputStream.read(bArr, i15, i10 - i15);
                if (i17 == -1) {
                    throw InvalidProtocolBufferException.j();
                }
                this.f23923k += i17;
                i15 += i17;
            } catch (InvalidProtocolBufferException e11) {
                e11.h();
                throw e11;
            }
        }
        return bArr;
    }

    public final ArrayList C(int i10) throws IOException {
        ArrayList arrayList = new ArrayList();
        while (i10 > 0) {
            int iMin = Math.min(i10, 4096);
            byte[] bArr = new byte[iMin];
            int i11 = 0;
            while (i11 < iMin) {
                int i12 = this.f23917e.read(bArr, i11, iMin - i11);
                if (i12 == -1) {
                    throw InvalidProtocolBufferException.j();
                }
                this.f23923k += i12;
                i11 += i12;
            }
            i10 -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int D() throws InvalidProtocolBufferException {
        int i10 = this.f23921i;
        if (this.f23919g - i10 < 4) {
            J(4);
            i10 = this.f23921i;
        }
        this.f23921i = i10 + 4;
        byte[] bArr = this.f23918f;
        return ((bArr[i10 + 3] & 255) << 24) | (bArr[i10] & 255) | ((bArr[i10 + 1] & 255) << 8) | ((bArr[i10 + 2] & 255) << 16);
    }

    public final long E() throws InvalidProtocolBufferException {
        int i10 = this.f23921i;
        if (this.f23919g - i10 < 8) {
            J(8);
            i10 = this.f23921i;
        }
        this.f23921i = i10 + 8;
        byte[] bArr = this.f23918f;
        return ((((long) bArr[i10 + 7]) & 255) << 56) | (((long) bArr[i10]) & 255) | ((((long) bArr[i10 + 1]) & 255) << 8) | ((((long) bArr[i10 + 2]) & 255) << 16) | ((((long) bArr[i10 + 3]) & 255) << 24) | ((((long) bArr[i10 + 4]) & 255) << 32) | ((((long) bArr[i10 + 5]) & 255) << 40) | ((((long) bArr[i10 + 6]) & 255) << 48);
    }

    public final int F() {
        int i10;
        int i11 = this.f23921i;
        int i12 = this.f23919g;
        if (i12 != i11) {
            int i13 = i11 + 1;
            byte[] bArr = this.f23918f;
            byte b10 = bArr[i11];
            if (b10 >= 0) {
                this.f23921i = i13;
                return b10;
            }
            if (i12 - i13 >= 9) {
                int i14 = i11 + 2;
                int i15 = (bArr[i13] << 7) ^ b10;
                if (i15 < 0) {
                    i10 = i15 ^ (-128);
                } else {
                    int i16 = i11 + 3;
                    int i17 = (bArr[i14] << 14) ^ i15;
                    if (i17 >= 0) {
                        i10 = i17 ^ 16256;
                    } else {
                        int i18 = i11 + 4;
                        int i19 = i17 ^ (bArr[i16] << 21);
                        if (i19 < 0) {
                            i10 = (-2080896) ^ i19;
                        } else {
                            i16 = i11 + 5;
                            byte b11 = bArr[i18];
                            int i20 = (i19 ^ (b11 << 28)) ^ 266354560;
                            if (b11 < 0) {
                                i18 = i11 + 6;
                                if (bArr[i16] < 0) {
                                    i16 = i11 + 7;
                                    if (bArr[i18] < 0) {
                                        i18 = i11 + 8;
                                        if (bArr[i16] < 0) {
                                            i16 = i11 + 9;
                                            if (bArr[i18] < 0) {
                                                int i21 = i11 + 10;
                                                if (bArr[i16] >= 0) {
                                                    i14 = i21;
                                                    i10 = i20;
                                                }
                                            }
                                        }
                                    }
                                }
                                i10 = i20;
                            }
                            i10 = i20;
                        }
                        i14 = i18;
                    }
                    i14 = i16;
                }
                this.f23921i = i14;
                return i10;
            }
        }
        return (int) H();
    }

    public final long G() {
        long j10;
        long j11;
        long j12;
        long j13;
        int i10 = this.f23921i;
        int i11 = this.f23919g;
        if (i11 != i10) {
            int i12 = i10 + 1;
            byte[] bArr = this.f23918f;
            byte b10 = bArr[i10];
            if (b10 >= 0) {
                this.f23921i = i12;
                return b10;
            }
            if (i11 - i12 >= 9) {
                int i13 = i10 + 2;
                int i14 = (bArr[i12] << 7) ^ b10;
                if (i14 < 0) {
                    j10 = i14 ^ (-128);
                } else {
                    int i15 = i10 + 3;
                    int i16 = (bArr[i13] << 14) ^ i14;
                    if (i16 >= 0) {
                        j10 = i16 ^ 16256;
                        i13 = i15;
                    } else {
                        int i17 = i10 + 4;
                        int i18 = i16 ^ (bArr[i15] << 21);
                        if (i18 < 0) {
                            j13 = (-2080896) ^ i18;
                        } else {
                            long j14 = i18;
                            i13 = i10 + 5;
                            long j15 = j14 ^ (((long) bArr[i17]) << 28);
                            if (j15 >= 0) {
                                j12 = 266354560;
                            } else {
                                i17 = i10 + 6;
                                long j16 = j15 ^ (((long) bArr[i13]) << 35);
                                if (j16 < 0) {
                                    j11 = -34093383808L;
                                } else {
                                    i13 = i10 + 7;
                                    j15 = j16 ^ (((long) bArr[i17]) << 42);
                                    if (j15 >= 0) {
                                        j12 = 4363953127296L;
                                    } else {
                                        i17 = i10 + 8;
                                        j16 = j15 ^ (((long) bArr[i13]) << 49);
                                        if (j16 < 0) {
                                            j11 = -558586000294016L;
                                        } else {
                                            i13 = i10 + 9;
                                            long j17 = (j16 ^ (((long) bArr[i17]) << 56)) ^ 71499008037633920L;
                                            if (j17 < 0) {
                                                int i19 = i10 + 10;
                                                if (bArr[i13] >= 0) {
                                                    i13 = i19;
                                                }
                                            }
                                            j10 = j17;
                                        }
                                    }
                                }
                                j13 = j11 ^ j16;
                            }
                            j10 = j12 ^ j15;
                        }
                        i13 = i17;
                        j10 = j13;
                    }
                }
                this.f23921i = i13;
                return j10;
            }
        }
        return H();
    }

    public final long H() throws InvalidProtocolBufferException {
        long j10 = 0;
        for (int i10 = 0; i10 < 64; i10 += 7) {
            if (this.f23921i == this.f23919g) {
                J(1);
            }
            int i11 = this.f23921i;
            this.f23921i = i11 + 1;
            byte b10 = this.f23918f[i11];
            j10 |= ((long) (b10 & 127)) << i10;
            if ((b10 & 128) == 0) {
                return j10;
            }
        }
        throw InvalidProtocolBufferException.e();
    }

    public final void I() {
        int i10 = this.f23919g + this.f23920h;
        this.f23919g = i10;
        int i11 = this.f23923k + i10;
        int i12 = this.f23924l;
        if (i11 <= i12) {
            this.f23920h = 0;
            return;
        }
        int i13 = i11 - i12;
        this.f23920h = i13;
        this.f23919g = i10 - i13;
    }

    public final void J(int i10) throws InvalidProtocolBufferException {
        if (L(i10)) {
            return;
        }
        if (i10 <= (this.f23930c - this.f23923k) - this.f23921i) {
            throw InvalidProtocolBufferException.j();
        }
        throw new InvalidProtocolBufferException("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit. If reading multiple messages, consider resetting the counter between each message using CodedInputStream.resetSizeCounter().");
    }

    public final void K(int i10) throws InvalidProtocolBufferException {
        int i11 = this.f23919g;
        int i12 = this.f23921i;
        if (i10 <= i11 - i12 && i10 >= 0) {
            this.f23921i = i12 + i10;
            return;
        }
        InputStream inputStream = this.f23917e;
        if (i10 < 0) {
            throw InvalidProtocolBufferException.f();
        }
        int i13 = this.f23923k;
        int i14 = i13 + i12;
        int i15 = i14 + i10;
        int i16 = this.f23924l;
        if (i15 > i16) {
            K((i16 - i13) - i12);
            throw InvalidProtocolBufferException.j();
        }
        this.f23923k = i14;
        int i17 = i11 - i12;
        this.f23919g = 0;
        this.f23921i = 0;
        while (i17 < i10) {
            long j10 = i10 - i17;
            try {
                try {
                    long jSkip = inputStream.skip(j10);
                    if (jSkip < 0 || jSkip > j10) {
                        throw new IllegalStateException(inputStream.getClass() + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i17 += (int) jSkip;
                    }
                } catch (InvalidProtocolBufferException e10) {
                    e10.h();
                    throw e10;
                }
            } catch (Throwable th) {
                this.f23923k += i17;
                I();
                throw th;
            }
        }
        this.f23923k += i17;
        I();
        if (i17 >= i10) {
            return;
        }
        int i18 = this.f23919g;
        int i19 = i18 - this.f23921i;
        this.f23921i = i18;
        J(1);
        while (true) {
            int i20 = i10 - i19;
            int i21 = this.f23919g;
            if (i20 <= i21) {
                this.f23921i = i20;
                return;
            } else {
                i19 += i21;
                this.f23921i = i21;
                J(1);
            }
        }
    }

    public final boolean L(int i10) throws IOException {
        int i11 = this.f23921i;
        int i12 = i11 + i10;
        int i13 = this.f23919g;
        if (i12 <= i13) {
            throw new IllegalStateException(defpackage.e.e("refillBuffer() called when ", i10, " bytes were already available in buffer"));
        }
        int i14 = this.f23923k;
        int i15 = this.f23930c;
        if (i10 > (i15 - i14) - i11 || i14 + i11 + i10 > this.f23924l) {
            return false;
        }
        byte[] bArr = this.f23918f;
        if (i11 > 0) {
            if (i13 > i11) {
                System.arraycopy(bArr, i11, bArr, 0, i13 - i11);
            }
            this.f23923k += i11;
            this.f23919g -= i11;
            this.f23921i = 0;
        }
        int i16 = this.f23919g;
        int iMin = Math.min(bArr.length - i16, (i15 - this.f23923k) - i16);
        InputStream inputStream = this.f23917e;
        try {
            int i17 = inputStream.read(bArr, i16, iMin);
            if (i17 != 0 && i17 >= -1 && i17 <= bArr.length) {
                if (i17 <= 0) {
                    return false;
                }
                this.f23919g += i17;
                I();
                return this.f23919g >= i10 || L(i10);
            }
            throw new IllegalStateException(inputStream.getClass() + "#read(byte[]) returned invalid result: " + i17 + "\nThe InputStream implementation is buggy.");
        } catch (InvalidProtocolBufferException e10) {
            e10.h();
            throw e10;
        }
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final void a(int i10) throws InvalidProtocolBufferException {
        if (this.f23922j != i10) {
            throw new InvalidProtocolBufferException("Protocol message end-group tag did not match expected tag.");
        }
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int d() {
        return this.f23923k + this.f23921i;
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final boolean e() {
        return this.f23921i == this.f23919g && !L(1);
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final void g(int i10) {
        this.f23924l = i10;
        I();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int h(int i10) throws InvalidProtocolBufferException {
        if (i10 < 0) {
            throw InvalidProtocolBufferException.f();
        }
        int i11 = this.f23923k + this.f23921i + i10;
        if (i11 < 0) {
            throw new InvalidProtocolBufferException("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit. If reading multiple messages, consider resetting the counter between each message using CodedInputStream.resetSizeCounter().");
        }
        int i12 = this.f23924l;
        if (i11 > i12) {
            throw InvalidProtocolBufferException.j();
        }
        this.f23924l = i11;
        I();
        return i12;
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final boolean i() {
        return G() != 0;
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final ByteString j() throws IOException {
        int iF = F();
        int i10 = this.f23919g;
        int i11 = this.f23921i;
        int i12 = i10 - i11;
        byte[] bArr = this.f23918f;
        if (iF <= i12 && iF > 0) {
            ByteString byteStringG = ByteString.g(i11, iF, bArr);
            this.f23921i += iF;
            return byteStringG;
        }
        if (iF == 0) {
            return ByteString.f23846a;
        }
        if (iF < 0) {
            throw InvalidProtocolBufferException.f();
        }
        byte[] bArrB = B(iF);
        if (bArrB != null) {
            return ByteString.g(0, bArrB.length, bArrB);
        }
        int i13 = this.f23921i;
        int i14 = this.f23919g;
        int length = i14 - i13;
        this.f23923k += i14;
        this.f23921i = 0;
        this.f23919g = 0;
        ArrayList<byte[]> arrayListC = C(iF - length);
        byte[] bArr2 = new byte[iF];
        System.arraycopy(bArr, i13, bArr2, 0, length);
        for (byte[] bArr3 : arrayListC) {
            System.arraycopy(bArr3, 0, bArr2, length, bArr3.length);
            length += bArr3.length;
        }
        ByteString byteString = ByteString.f23846a;
        return new ByteString.LiteralByteString(bArr2);
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final double k() {
        return Double.longBitsToDouble(E());
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int l() {
        return F();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int m() {
        return D();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final long n() {
        return E();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final float o() {
        return Float.intBitsToFloat(D());
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int p() {
        return F();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final long q() {
        return G();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int r() {
        return D();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final long s() {
        return E();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int t() {
        return l.b(F());
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final long u() {
        return l.c(G());
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final String v() throws InvalidProtocolBufferException {
        int iF = F();
        byte[] bArr = this.f23918f;
        if (iF > 0) {
            int i10 = this.f23919g;
            int i11 = this.f23921i;
            if (iF <= i10 - i11) {
                String str = new String(bArr, i11, iF, b0.f23882a);
                this.f23921i += iF;
                return str;
            }
        }
        if (iF == 0) {
            return "";
        }
        if (iF < 0) {
            throw InvalidProtocolBufferException.f();
        }
        if (iF > this.f23919g) {
            return new String(A(iF), b0.f23882a);
        }
        J(iF);
        String str2 = new String(bArr, this.f23921i, iF, b0.f23882a);
        this.f23921i += iF;
        return str2;
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final String w() throws IOException {
        int iF = F();
        int i10 = this.f23921i;
        int i11 = this.f23919g;
        int i12 = i11 - i10;
        byte[] bArrA = this.f23918f;
        if (iF <= i12 && iF > 0) {
            this.f23921i = i10 + iF;
        } else {
            if (iF == 0) {
                return "";
            }
            if (iF < 0) {
                throw InvalidProtocolBufferException.f();
            }
            i10 = 0;
            if (iF <= i11) {
                J(iF);
                this.f23921i = iF;
            } else {
                bArrA = A(iF);
            }
        }
        return n1.f23950a.W(i10, iF, bArrA);
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int x() throws InvalidProtocolBufferException {
        if (e()) {
            this.f23922j = 0;
            return 0;
        }
        int iF = F();
        this.f23922j = iF;
        if ((iF >>> 3) != 0) {
            return iF;
        }
        throw InvalidProtocolBufferException.b();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final int y() {
        return F();
    }

    @Override // com.google.crypto.tink.shaded.protobuf.l
    public final long z() {
        return G();
    }
}
