package com.google.android.gms.internal.ads;

import androidx.media3.common.C;
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 la2 extends ma2 {

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

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

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

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

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

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

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

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

    public /* synthetic */ la2(InputStream inputStream) {
        Charset charset = kb2.f13211a;
        this.f13578d = inputStream;
        this.f13579e = new byte[4096];
        this.f13580f = 0;
        this.f13582h = 0;
        this.f13584j = 0;
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int A() {
        return ma2.g(K());
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final long B() {
        return ma2.h(L());
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int C(int i10) throws zzicg {
        if (i10 < 0) {
            throw new zzicg("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
        }
        int i11 = this.f13584j + this.f13582h + i10;
        if (i11 < 0) {
            throw new zzicg("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.f13585k;
        if (i11 > i12) {
            throw new zzicg("While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length.");
        }
        this.f13585k = i11;
        E();
        return i12;
    }

    public final void D(int i10) throws zzicg {
        InputStream inputStream = this.f13578d;
        int i11 = this.f13580f;
        int i12 = this.f13582h;
        int i13 = i11 - i12;
        if (i10 <= i13 && i10 >= 0) {
            this.f13582h = i12 + i10;
            return;
        }
        if (i10 < 0) {
            throw new zzicg("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
        }
        int i14 = this.f13584j;
        int i15 = i14 + i12;
        int i16 = this.f13585k;
        if (i15 + i10 > i16) {
            D((i16 - i14) - i12);
            throw new zzicg("While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length.");
        }
        this.f13584j = i15;
        this.f13580f = 0;
        this.f13582h = 0;
        while (i13 < i10) {
            long j10 = i10 - i13;
            try {
                try {
                    long jSkip = inputStream.skip(j10);
                    if (jSkip < 0 || jSkip > j10) {
                        String strValueOf = String.valueOf(inputStream.getClass());
                        StringBuilder sb = new StringBuilder(strValueOf.length() + 31 + String.valueOf(jSkip).length() + 41);
                        sb.append(strValueOf);
                        sb.append("#skip returned invalid result: ");
                        sb.append(jSkip);
                        sb.append("\nThe InputStream implementation is buggy.");
                        throw new IllegalStateException(sb.toString());
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i13 += (int) jSkip;
                    }
                } catch (zzicg e10) {
                    e10.a();
                    throw e10;
                }
            } catch (Throwable th) {
                this.f13584j += i13;
                E();
                throw th;
            }
        }
        this.f13584j += i13;
        E();
        if (i13 >= i10) {
            return;
        }
        int i17 = this.f13580f;
        int i18 = i17 - this.f13582h;
        this.f13582h = i17;
        F(1);
        while (true) {
            int i19 = i10 - i18;
            int i20 = this.f13580f;
            if (i19 <= i20) {
                this.f13582h = i19;
                return;
            } else {
                i18 += i20;
                this.f13582h = i20;
                F(1);
            }
        }
    }

    public final void E() {
        int i10 = this.f13580f + this.f13581g;
        this.f13580f = i10;
        int i11 = this.f13584j + i10;
        int i12 = this.f13585k;
        if (i11 <= i12) {
            this.f13581g = 0;
            return;
        }
        int i13 = i11 - i12;
        this.f13581g = i13;
        this.f13580f = i10 - i13;
    }

    public final void F(int i10) throws zzicg {
        if (G(i10)) {
            return;
        }
        if (i10 <= (Integer.MAX_VALUE - this.f13584j) - this.f13582h) {
            throw new zzicg("While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length.");
        }
        throw new zzicg("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 boolean G(int i10) throws IOException {
        InputStream inputStream = this.f13578d;
        int i11 = this.f13582h;
        int i12 = i11 + i10;
        int i13 = this.f13580f;
        if (i12 <= i13) {
            throw new IllegalStateException(com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.g(new StringBuilder(String.valueOf(i10).length() + 66), "refillBuffer() called when ", i10, " bytes were already available in buffer"));
        }
        int i14 = this.f13584j;
        if (i10 > (Integer.MAX_VALUE - i14) - i11 || i14 + i11 + i10 > this.f13585k) {
            return false;
        }
        byte[] bArr = this.f13579e;
        if (i11 > 0) {
            if (i13 > i11) {
                System.arraycopy(bArr, i11, bArr, 0, i13 - i11);
            }
            i14 = this.f13584j + i11;
            this.f13584j = i14;
            i13 = this.f13580f - i11;
            this.f13580f = i13;
            this.f13582h = 0;
        }
        try {
            int i15 = inputStream.read(bArr, i13, Math.min(4096 - i13, (Integer.MAX_VALUE - i14) - i13));
            if (i15 != 0 && i15 >= -1 && i15 <= 4096) {
                if (i15 <= 0) {
                    return false;
                }
                this.f13580f += i15;
                E();
                return this.f13580f >= i10 || G(i10);
            }
            String strValueOf = String.valueOf(inputStream.getClass());
            StringBuilder sb = new StringBuilder(String.valueOf(i15).length() + strValueOf.length() + 39 + 41);
            sb.append(strValueOf);
            sb.append("#read(byte[]) returned invalid result: ");
            sb.append(i15);
            sb.append("\nThe InputStream implementation is buggy.");
            throw new IllegalStateException(sb.toString());
        } catch (zzicg e10) {
            e10.a();
            throw e10;
        }
    }

    public final byte[] H(int i10) throws IOException {
        byte[] bArrI = I(i10);
        if (bArrI != null) {
            return bArrI;
        }
        int i11 = this.f13582h;
        int i12 = this.f13580f;
        int i13 = i12 - i11;
        this.f13584j += i12;
        this.f13582h = 0;
        this.f13580f = 0;
        ArrayList<byte[]> arrayListJ = J(i10 - i13);
        byte[] bArr = new byte[i10];
        System.arraycopy(this.f13579e, i11, bArr, 0, i13);
        for (byte[] bArr2 : arrayListJ) {
            int length = bArr2.length;
            System.arraycopy(bArr2, 0, bArr, i13, length);
            i13 += length;
        }
        return bArr;
    }

    public final byte[] I(int i10) throws IOException {
        if (i10 == 0) {
            return kb2.f13212b;
        }
        int i11 = this.f13584j;
        int i12 = this.f13582h;
        int i13 = i11 + i12 + i10;
        if (C.RATE_UNSET_INT + i13 > 0) {
            throw new zzicg("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.f13585k;
        if (i13 > i14) {
            D((i14 - i11) - i12);
            throw new zzicg("While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length.");
        }
        int i15 = this.f13580f - i12;
        int i16 = i10 - i15;
        InputStream inputStream = this.f13578d;
        if (i16 >= 4096) {
            try {
                if (i16 > inputStream.available()) {
                    return null;
                }
            } catch (zzicg e10) {
                e10.a();
                throw e10;
            }
        }
        byte[] bArr = new byte[i10];
        System.arraycopy(this.f13579e, this.f13582h, bArr, 0, i15);
        this.f13584j += this.f13580f;
        this.f13582h = 0;
        this.f13580f = 0;
        while (i15 < i10) {
            try {
                int i17 = inputStream.read(bArr, i15, i10 - i15);
                if (i17 == -1) {
                    throw new zzicg("While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length.");
                }
                this.f13584j += i17;
                i15 += i17;
            } catch (zzicg e11) {
                e11.a();
                throw e11;
            }
        }
        return bArr;
    }

    public final ArrayList J(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.f13578d.read(bArr, i11, iMin - i11);
                if (i12 == -1) {
                    throw new zzicg("While parsing a protocol message, the input ended unexpectedly in the middle of a field.  This could mean either that the input has been truncated or that an embedded message misreported its own length.");
                }
                this.f13584j += i12;
                i11 += i12;
            }
            i10 -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int K() {
        int i10;
        int i11 = this.f13582h;
        int i12 = this.f13580f;
        if (i12 != i11) {
            int i13 = i11 + 1;
            byte[] bArr = this.f13579e;
            byte b10 = bArr[i11];
            if (b10 >= 0) {
                this.f13582h = 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.f13582h = i14;
                return i10;
            }
        }
        return (int) M();
    }

    public final long L() {
        long j10;
        long j11;
        long j12;
        int i10 = this.f13582h;
        int i11 = this.f13580f;
        if (i11 != i10) {
            int i12 = i10 + 1;
            byte[] bArr = this.f13579e;
            byte b10 = bArr[i10];
            if (b10 >= 0) {
                this.f13582h = 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;
                    } else {
                        int i17 = i10 + 4;
                        int i18 = i16 ^ (bArr[i15] << 21);
                        if (i18 < 0) {
                            long j13 = (-2080896) ^ i18;
                            i13 = i17;
                            j10 = j13;
                        } else {
                            i15 = i10 + 5;
                            long j14 = ((long) i18) ^ (((long) bArr[i17]) << 28);
                            if (j14 >= 0) {
                                j11 = 266354560;
                            } else {
                                int i19 = i10 + 6;
                                long j15 = j14 ^ (((long) bArr[i15]) << 35);
                                if (j15 < 0) {
                                    j12 = -34093383808L;
                                } else {
                                    i15 = i10 + 7;
                                    j14 = j15 ^ (((long) bArr[i19]) << 42);
                                    if (j14 >= 0) {
                                        j11 = 4363953127296L;
                                    } else {
                                        i19 = i10 + 8;
                                        j15 = j14 ^ (((long) bArr[i15]) << 49);
                                        if (j15 < 0) {
                                            j12 = -558586000294016L;
                                        } else {
                                            i15 = i10 + 9;
                                            j14 = j15 ^ (((long) bArr[i19]) << 56);
                                            if (j14 >= 0) {
                                                j11 = 71499008037633920L;
                                            } else {
                                                int i20 = i10 + 10;
                                                long j16 = j14 ^ (((long) bArr[i15]) << 63);
                                                if (j16 >= 0) {
                                                    j10 = j16 ^ (-9151873028817141888L);
                                                    i13 = i20;
                                                }
                                            }
                                        }
                                    }
                                }
                                j10 = j15 ^ j12;
                                i13 = i19;
                            }
                            j10 = j14 ^ j11;
                        }
                    }
                    i13 = i15;
                }
                this.f13582h = i13;
                return j10;
            }
        }
        return M();
    }

    public final long M() throws zzicg {
        long j10 = 0;
        for (int i10 = 0; i10 < 64; i10 += 7) {
            if (this.f13582h == this.f13580f) {
                F(1);
            }
            int i11 = this.f13582h;
            this.f13582h = i11 + 1;
            byte b10 = this.f13579e[i11];
            j10 |= ((long) (b10 & 127)) << i10;
            if ((b10 & 128) == 0) {
                return j10;
            }
        }
        throw new zzicg("CodedInputStream encountered a malformed varint.");
    }

    public final int N() throws zzicg {
        int i10 = this.f13582h;
        if (this.f13580f - i10 < 4) {
            F(4);
            i10 = this.f13582h;
        }
        this.f13582h = i10 + 4;
        byte[] bArr = this.f13579e;
        int i11 = bArr[i10] & 255;
        int i12 = bArr[i10 + 1] & 255;
        int i13 = bArr[i10 + 2] & 255;
        return ((bArr[i10 + 3] & 255) << 24) | (i12 << 8) | i11 | (i13 << 16);
    }

    public final long O() throws zzicg {
        int i10 = this.f13582h;
        if (this.f13580f - i10 < 8) {
            F(8);
            i10 = this.f13582h;
        }
        this.f13582h = i10 + 8;
        byte[] bArr = this.f13579e;
        long j10 = bArr[i10];
        long j11 = (((long) bArr[i10 + 1]) & 255) << 8;
        long j12 = bArr[i10 + 2];
        long j13 = bArr[i10 + 3];
        return ((((long) bArr[i10 + 7]) & 255) << 56) | (j10 & 255) | j11 | ((j12 & 255) << 16) | ((j13 & 255) << 24) | ((bArr[i10 + 4] & 255) << 32) | ((bArr[i10 + 5] & 255) << 40) | ((bArr[i10 + 6] & 255) << 48);
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final void a(int i10) {
        this.f13585k = i10;
        E();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final boolean b() {
        return this.f13582h == this.f13580f && !G(1);
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int c() {
        return this.f13584j + this.f13582h;
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int i() throws zzicg {
        if (b()) {
            this.f13583i = 0;
            return 0;
        }
        int iK = K();
        this.f13583i = iK;
        if ((iK >>> 3) != 0) {
            return iK;
        }
        throw new zzicg("Protocol message contained an invalid tag (zero).");
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final void j(int i10) throws zzicg {
        if (this.f13583i != i10) {
            throw new zzicg("Protocol message end-group tag did not match expected tag.");
        }
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final boolean k(int i10) throws zzicg {
        int i11 = i10 & 7;
        int i12 = 0;
        if (i11 != 0) {
            if (i11 == 1) {
                D(8);
                return true;
            }
            if (i11 == 2) {
                D(K());
                return true;
            }
            if (i11 == 3) {
                f();
                j(((i10 >>> 3) << 3) | 4);
                return true;
            }
            if (i11 == 4) {
                if (this.f14050b == 0) {
                    j(0);
                }
                return false;
            }
            if (i11 != 5) {
                throw new zzicf();
            }
            D(4);
            return true;
        }
        int i13 = this.f13580f - this.f13582h;
        byte[] bArr = this.f13579e;
        if (i13 >= 10) {
            while (i12 < 10) {
                int i14 = this.f13582h;
                this.f13582h = i14 + 1;
                if (bArr[i14] < 0) {
                    i12++;
                }
            }
            throw new zzicg("CodedInputStream encountered a malformed varint.");
        }
        while (i12 < 10) {
            if (this.f13582h == this.f13580f) {
                F(1);
            }
            int i15 = this.f13582h;
            this.f13582h = i15 + 1;
            if (bArr[i15] < 0) {
                i12++;
            }
        }
        throw new zzicg("CodedInputStream encountered a malformed varint.");
        return true;
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final double l() {
        return Double.longBitsToDouble(O());
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final float m() {
        return Float.intBitsToFloat(N());
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final long n() {
        return L();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final long o() {
        return L();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int p() {
        return K();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final long q() {
        return O();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int r() {
        return N();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final boolean s() {
        return L() != 0;
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final String t() throws zzicg {
        int iK = K();
        byte[] bArr = this.f13579e;
        if (iK > 0) {
            int i10 = this.f13580f;
            int i11 = this.f13582h;
            if (iK <= i10 - i11) {
                String str = new String(bArr, i11, iK, kb2.f13211a);
                this.f13582h += iK;
                return str;
            }
        }
        if (iK == 0) {
            return "";
        }
        if (iK < 0) {
            throw new zzicg("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
        }
        if (iK > this.f13580f) {
            return new String(H(iK), kb2.f13211a);
        }
        F(iK);
        String str2 = new String(bArr, this.f13582h, iK, kb2.f13211a);
        this.f13582h += iK;
        return str2;
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final String u() throws IOException {
        int iK = K();
        int i10 = this.f13582h;
        int i11 = this.f13580f;
        int i12 = i11 - i10;
        byte[] bArrH = this.f13579e;
        if (iK <= i12 && iK > 0) {
            this.f13582h = i10 + iK;
        } else {
            if (iK == 0) {
                return "";
            }
            if (iK < 0) {
                throw new zzicg("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
            }
            i10 = 0;
            if (iK <= i11) {
                F(iK);
                this.f13582h = iK;
            } else {
                bArrH = H(iK);
            }
        }
        return tc2.d(i10, iK, bArrH);
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final zzian v() throws IOException {
        int iK = K();
        int i10 = this.f13580f;
        int i11 = this.f13582h;
        int i12 = i10 - i11;
        byte[] bArr = this.f13579e;
        if (iK <= i12 && iK > 0) {
            zzian zzianVarX = zzian.x(bArr, i11, iK);
            this.f13582h += iK;
            return zzianVarX;
        }
        if (iK == 0) {
            return zzian.f19856a;
        }
        if (iK < 0) {
            throw new zzicg("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
        }
        byte[] bArrI = I(iK);
        if (bArrI != null) {
            return zzian.x(bArrI, 0, bArrI.length);
        }
        int i13 = this.f13582h;
        int i14 = this.f13580f;
        int i15 = i14 - i13;
        this.f13584j += i14;
        this.f13582h = 0;
        this.f13580f = 0;
        ArrayList<byte[]> arrayListJ = J(iK - i15);
        byte[] bArr2 = new byte[iK];
        System.arraycopy(bArr, i13, bArr2, 0, i15);
        for (byte[] bArr3 : arrayListJ) {
            int length = bArr3.length;
            System.arraycopy(bArr3, 0, bArr2, i15, length);
            i15 += length;
        }
        zzian zzianVar = zzian.f19856a;
        try {
            return new zzial(bArr2);
        } catch (zzicg e10) {
            throw new AssertionError("Expected no InvalidProtocolBufferException as data UTF8 validity is not checked.", e10);
        }
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int w() {
        return K();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int x() {
        return K();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final int y() {
        return N();
    }

    @Override // com.google.android.gms.internal.ads.ma2
    public final long z() {
        return O();
    }
}
