package com.google.android.recaptcha.internal;

import Z0.o;
import androidx.media3.common.C;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/* JADX INFO: loaded from: classes3.dex */
final class zzlg extends zzli {
    private final InputStream zze;
    private final byte[] zzf;
    private int zzg;
    private int zzh;
    private int zzi;
    private int zzj;
    private int zzk;
    private int zzl;

    public /* synthetic */ zzlg(InputStream inputStream, int i6, zzlh zzlhVar) {
        super(null);
        this.zzl = Integer.MAX_VALUE;
        byte[] bArr = zznl.zzb;
        this.zze = inputStream;
        this.zzf = new byte[4096];
        this.zzg = 0;
        this.zzi = 0;
        this.zzk = 0;
    }

    private final List zzJ(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.zze.read(bArr, i7, iMin - i7);
                if (i8 == -1) {
                    throw new zznn("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.zzk += i8;
                i7 += i8;
            }
            i6 -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    private final void zzK() {
        int i6 = this.zzg + this.zzh;
        this.zzg = i6;
        int i7 = this.zzk + i6;
        int i8 = this.zzl;
        if (i7 <= i8) {
            this.zzh = 0;
            return;
        }
        int i9 = i7 - i8;
        this.zzh = i9;
        this.zzg = i6 - i9;
    }

    private final void zzL(int i6) throws zznn {
        if (zzM(i6)) {
            return;
        }
        if (i6 <= (Integer.MAX_VALUE - this.zzk) - this.zzi) {
            throw new zznn("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 zznn("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
    }

    private final boolean zzM(int i6) throws IOException {
        int i7 = this.zzi;
        int i8 = i7 + i6;
        int i9 = this.zzg;
        if (i8 <= i9) {
            throw new IllegalStateException(o.j(i6, "refillBuffer() called when ", " bytes were already available in buffer"));
        }
        int i10 = this.zzk;
        if (i6 > (Integer.MAX_VALUE - i10) - i7 || i10 + i7 + i6 > this.zzl) {
            return false;
        }
        if (i7 > 0) {
            if (i9 > i7) {
                byte[] bArr = this.zzf;
                System.arraycopy(bArr, i7, bArr, 0, i9 - i7);
            }
            i10 = this.zzk + i7;
            this.zzk = i10;
            i9 = this.zzg - i7;
            this.zzg = i9;
            this.zzi = 0;
        }
        try {
            int i11 = this.zze.read(this.zzf, i9, Math.min(4096 - i9, (Integer.MAX_VALUE - i10) - i9));
            if (i11 == 0 || i11 < -1 || i11 > 4096) {
                throw new IllegalStateException(String.valueOf(this.zze.getClass()) + "#read(byte[]) returned invalid result: " + i11 + "\nThe InputStream implementation is buggy.");
            }
            if (i11 <= 0) {
                return false;
            }
            this.zzg += i11;
            zzK();
            if (this.zzg >= i6) {
                return true;
            }
            return zzM(i6);
        } catch (zznn e6) {
            e6.zza();
            throw e6;
        }
    }

    private final byte[] zzN(int i6, boolean z6) throws IOException {
        byte[] bArrZzO = zzO(i6);
        if (bArrZzO != null) {
            return bArrZzO;
        }
        int i7 = this.zzi;
        int i8 = this.zzg;
        int i9 = i8 - i7;
        this.zzk += i8;
        this.zzi = 0;
        this.zzg = 0;
        List<byte[]> listZzJ = zzJ(i6 - i9);
        byte[] bArr = new byte[i6];
        System.arraycopy(this.zzf, i7, bArr, 0, i9);
        for (byte[] bArr2 : listZzJ) {
            int length = bArr2.length;
            System.arraycopy(bArr2, 0, bArr, i9, length);
            i9 += length;
        }
        return bArr;
    }

    private final byte[] zzO(int i6) throws IOException {
        if (i6 == 0) {
            return zznl.zzb;
        }
        int i7 = this.zzk;
        int i8 = this.zzi;
        int i9 = i7 + i8 + i6;
        if (C.RATE_UNSET_INT + i9 > 0) {
            throw new zznn("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
        }
        int i10 = this.zzl;
        if (i9 > i10) {
            zzB((i10 - i7) - i8);
            throw new zznn("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 i11 = this.zzg - i8;
        int i12 = i6 - i11;
        if (i12 >= 4096) {
            try {
                if (i12 > this.zze.available()) {
                    return null;
                }
            } catch (zznn e6) {
                e6.zza();
                throw e6;
            }
        }
        byte[] bArr = new byte[i6];
        System.arraycopy(this.zzf, this.zzi, bArr, 0, i11);
        this.zzk += this.zzg;
        this.zzi = 0;
        this.zzg = 0;
        while (i11 < i6) {
            try {
                int i13 = this.zze.read(bArr, i11, i6 - i11);
                if (i13 == -1) {
                    throw new zznn("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.zzk += i13;
                i11 += i13;
            } catch (zznn e7) {
                e7.zza();
                throw e7;
            }
        }
        return bArr;
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final void zzA(int i6) {
        this.zzl = i6;
        zzK();
    }

    public final void zzB(int i6) throws zznn {
        int i7 = this.zzg;
        int i8 = this.zzi;
        int i9 = i7 - i8;
        if (i6 <= i9 && i6 >= 0) {
            this.zzi = i8 + i6;
            return;
        }
        if (i6 < 0) {
            throw new zznn("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
        }
        int i10 = this.zzk;
        int i11 = i10 + i8;
        int i12 = this.zzl;
        if (i11 + i6 > i12) {
            zzB((i12 - i10) - i8);
            throw new zznn("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.zzk = i11;
        this.zzg = 0;
        this.zzi = 0;
        while (i9 < i6) {
            try {
                long j4 = i6 - i9;
                try {
                    long jSkip = this.zze.skip(j4);
                    if (jSkip < 0 || jSkip > j4) {
                        throw new IllegalStateException(String.valueOf(this.zze.getClass()) + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i9 += (int) jSkip;
                    }
                } catch (zznn e6) {
                    e6.zza();
                    throw e6;
                }
            } catch (Throwable th) {
                this.zzk += i9;
                zzK();
                throw th;
            }
        }
        this.zzk += i9;
        zzK();
        if (i9 >= i6) {
            return;
        }
        int i13 = this.zzg;
        int i14 = i13 - this.zzi;
        this.zzi = i13;
        zzL(1);
        while (true) {
            int i15 = i6 - i14;
            int i16 = this.zzg;
            if (i15 <= i16) {
                this.zzi = i15;
                return;
            } else {
                i14 += i16;
                this.zzi = i16;
                zzL(1);
            }
        }
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final boolean zzC() {
        return this.zzi == this.zzg && !zzM(1);
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final boolean zzD() {
        return zzr() != 0;
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final boolean zzE(int i6) throws zznn {
        int i7 = i6 & 7;
        int i8 = 0;
        if (i7 == 0) {
            if (this.zzg - this.zzi < 10) {
                while (i8 < 10) {
                    if (zza() < 0) {
                        i8++;
                    }
                }
                throw new zznn("CodedInputStream encountered a malformed varint.");
            }
            while (i8 < 10) {
                byte[] bArr = this.zzf;
                int i9 = this.zzi;
                this.zzi = i9 + 1;
                if (bArr[i9] < 0) {
                    i8++;
                }
            }
            throw new zznn("CodedInputStream encountered a malformed varint.");
            return true;
        }
        if (i7 == 1) {
            zzB(8);
            return true;
        }
        if (i7 == 2) {
            zzB(zzj());
            return true;
        }
        if (i7 == 3) {
            zzI();
            zzz(((i6 >>> 3) << 3) | 4);
            return true;
        }
        if (i7 == 4) {
            return false;
        }
        if (i7 != 5) {
            throw new zznm("Protocol message tag had invalid wire type.");
        }
        zzB(4);
        return true;
    }

    public final byte zza() throws zznn {
        if (this.zzi == this.zzg) {
            zzL(1);
        }
        byte[] bArr = this.zzf;
        int i6 = this.zzi;
        this.zzi = i6 + 1;
        return bArr[i6];
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final double zzb() {
        return Double.longBitsToDouble(zzq());
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final float zzc() {
        return Float.intBitsToFloat(zzi());
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zzd() {
        return this.zzk + this.zzi;
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zze(int i6) throws zznn {
        if (i6 < 0) {
            throw new zznn("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
        }
        int i7 = this.zzk + this.zzi + i6;
        if (i7 < 0) {
            throw new zznn("Failed to parse the message.");
        }
        int i8 = this.zzl;
        if (i7 > i8) {
            throw new zznn("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.zzl = i7;
        zzK();
        return i8;
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zzf() {
        return zzj();
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zzg() {
        return zzi();
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zzh() {
        return zzj();
    }

    public final int zzi() throws zznn {
        int i6 = this.zzi;
        if (this.zzg - i6 < 4) {
            zzL(4);
            i6 = this.zzi;
        }
        byte[] bArr = this.zzf;
        this.zzi = i6 + 4;
        int i7 = bArr[i6] & 255;
        int i8 = bArr[i6 + 1] & 255;
        int i9 = bArr[i6 + 2] & 255;
        return ((bArr[i6 + 3] & 255) << 24) | (i8 << 8) | i7 | (i9 << 16);
    }

    public final int zzj() {
        int i6;
        int i7 = this.zzi;
        int i8 = this.zzg;
        if (i8 != i7) {
            byte[] bArr = this.zzf;
            int i9 = i7 + 1;
            byte b5 = bArr[i7];
            if (b5 >= 0) {
                this.zzi = 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.zzi = i10;
                return i6;
            }
        }
        return (int) zzs();
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zzk() {
        return zzi();
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zzl() {
        return zzli.zzF(zzj());
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zzm() throws zznn {
        if (zzC()) {
            this.zzj = 0;
            return 0;
        }
        int iZzj = zzj();
        this.zzj = iZzj;
        if ((iZzj >>> 3) != 0) {
            return iZzj;
        }
        throw new zznn("Protocol message contained an invalid tag (zero).");
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final int zzn() {
        return zzj();
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final long zzo() {
        return zzq();
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final long zzp() {
        return zzr();
    }

    public final long zzq() throws zznn {
        int i6 = this.zzi;
        if (this.zzg - i6 < 8) {
            zzL(8);
            i6 = this.zzi;
        }
        byte[] bArr = this.zzf;
        this.zzi = i6 + 8;
        long j4 = bArr[i6];
        long j6 = (((long) bArr[i6 + 1]) & 255) << 8;
        long j7 = bArr[i6 + 2];
        long j8 = bArr[i6 + 3];
        return ((((long) bArr[i6 + 6]) & 255) << 48) | (j4 & 255) | j6 | ((j7 & 255) << 16) | ((j8 & 255) << 24) | ((bArr[i6 + 4] & 255) << 32) | ((bArr[i6 + 5] & 255) << 40) | ((((long) bArr[i6 + 7]) & 255) << 56);
    }

    public final long zzr() {
        long j4;
        long j6;
        int i6 = this.zzi;
        int i7 = this.zzg;
        if (i7 != i6) {
            byte[] bArr = this.zzf;
            int i8 = i6 + 1;
            byte b5 = bArr[i6];
            if (b5 >= 0) {
                this.zzi = 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;
                    } else {
                        int i13 = i6 + 4;
                        int i14 = i12 ^ (bArr[i11] << 21);
                        if (i14 < 0) {
                            long j7 = (-2080896) ^ i14;
                            i9 = i13;
                            j4 = j7;
                        } else {
                            i11 = i6 + 5;
                            long j8 = (((long) bArr[i13]) << 28) ^ ((long) i14);
                            if (j8 >= 0) {
                                j4 = j8 ^ 266354560;
                            } else {
                                i9 = i6 + 6;
                                long j9 = (((long) bArr[i11]) << 35) ^ j8;
                                if (j9 < 0) {
                                    j6 = -34093383808L;
                                } else {
                                    int i15 = i6 + 7;
                                    long j10 = j9 ^ (((long) bArr[i9]) << 42);
                                    if (j10 >= 0) {
                                        j4 = j10 ^ 4363953127296L;
                                    } else {
                                        i9 = i6 + 8;
                                        j9 = j10 ^ (((long) bArr[i15]) << 49);
                                        if (j9 < 0) {
                                            j6 = -558586000294016L;
                                        } else {
                                            i15 = i6 + 9;
                                            long j11 = (j9 ^ (((long) bArr[i9]) << 56)) ^ 71499008037633920L;
                                            if (j11 < 0) {
                                                i9 = i6 + 10;
                                                if (bArr[i15] >= 0) {
                                                    j4 = j11;
                                                }
                                            } else {
                                                j4 = j11;
                                            }
                                        }
                                    }
                                    i9 = i15;
                                }
                                j4 = j9 ^ j6;
                            }
                        }
                    }
                    i9 = i11;
                }
                this.zzi = i9;
                return j4;
            }
        }
        return zzs();
    }

    public final long zzs() throws zznn {
        long j4 = 0;
        for (int i6 = 0; i6 < 64; i6 += 7) {
            byte bZza = zza();
            j4 |= ((long) (bZza & 127)) << i6;
            if ((bZza & 128) == 0) {
                return j4;
            }
        }
        throw new zznn("CodedInputStream encountered a malformed varint.");
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final long zzt() {
        return zzq();
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final long zzu() {
        return zzli.zzG(zzr());
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final long zzv() {
        return zzr();
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final zzle zzw() throws IOException {
        int iZzj = zzj();
        int i6 = this.zzg;
        int i7 = this.zzi;
        if (iZzj <= i6 - i7 && iZzj > 0) {
            zzle zzleVarZzk = zzle.zzk(this.zzf, i7, iZzj);
            this.zzi += iZzj;
            return zzleVarZzk;
        }
        if (iZzj == 0) {
            return zzle.zzb;
        }
        if (iZzj < 0) {
            throw new zznn("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
        }
        byte[] bArrZzO = zzO(iZzj);
        if (bArrZzO != null) {
            return zzle.zzk(bArrZzO, 0, bArrZzO.length);
        }
        int i8 = this.zzi;
        int i9 = this.zzg;
        int i10 = i9 - i8;
        this.zzk += i9;
        this.zzi = 0;
        this.zzg = 0;
        List<byte[]> listZzJ = zzJ(iZzj - i10);
        byte[] bArr = new byte[iZzj];
        System.arraycopy(this.zzf, i8, bArr, 0, i10);
        for (byte[] bArr2 : listZzJ) {
            int length = bArr2.length;
            System.arraycopy(bArr2, 0, bArr, i10, length);
            i10 += length;
        }
        return new zzlc(bArr);
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final String zzx() throws zznn {
        int iZzj = zzj();
        if (iZzj > 0) {
            int i6 = this.zzg;
            int i7 = this.zzi;
            if (iZzj <= i6 - i7) {
                String str = new String(this.zzf, i7, iZzj, zznl.zza);
                this.zzi += iZzj;
                return str;
            }
        }
        if (iZzj == 0) {
            return "";
        }
        if (iZzj < 0) {
            throw new zznn("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
        }
        if (iZzj > this.zzg) {
            return new String(zzN(iZzj, false), zznl.zza);
        }
        zzL(iZzj);
        String str2 = new String(this.zzf, this.zzi, iZzj, zznl.zza);
        this.zzi += iZzj;
        return str2;
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final String zzy() throws IOException {
        byte[] bArrZzN;
        int iZzj = zzj();
        int i6 = this.zzi;
        int i7 = this.zzg;
        if (iZzj <= i7 - i6 && iZzj > 0) {
            bArrZzN = this.zzf;
            this.zzi = i6 + iZzj;
        } else {
            if (iZzj == 0) {
                return "";
            }
            if (iZzj < 0) {
                throw new zznn("CodedInputStream encountered an embedded string or message which claimed to have negative size.");
            }
            i6 = 0;
            if (iZzj <= i7) {
                zzL(iZzj);
                bArrZzN = this.zzf;
                this.zzi = iZzj;
            } else {
                bArrZzN = zzN(iZzj, false);
            }
        }
        return zzpv.zzd(bArrZzN, i6, iZzj);
    }

    @Override // com.google.android.recaptcha.internal.zzli
    public final void zzz(int i6) throws zznn {
        if (this.zzj != i6) {
            throw new zznn("Protocol message end-group tag did not match expected tag.");
        }
    }
}
