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

import Z0.o;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

    private final int zzaa() {
        int i6;
        int i7 = this.zzh;
        int i8 = this.zzf;
        if (i8 != i7) {
            byte[] bArr = this.zze;
            int i9 = i7 + 1;
            byte b5 = bArr[i7];
            if (b5 >= 0) {
                this.zzh = 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.zzh = i10;
                return i6;
            }
        }
        return (int) zzm();
    }

    private final long zzab() throws zzakm {
        int i6 = this.zzh;
        if (this.zzf - i6 < 8) {
            zzg(8);
            i6 = this.zzh;
        }
        byte[] bArr = this.zze;
        this.zzh = i6 + 8;
        return ((((long) bArr[i6 + 7]) & 255) << 56) | (((long) bArr[i6]) & 255) | ((((long) bArr[i6 + 1]) & 255) << 8) | ((((long) bArr[i6 + 2]) & 255) << 16) | ((((long) bArr[i6 + 3]) & 255) << 24) | ((((long) bArr[i6 + 4]) & 255) << 32) | ((((long) bArr[i6 + 5]) & 255) << 40) | ((((long) bArr[i6 + 6]) & 255) << 48);
    }

    private final long zzac() {
        long j4;
        long j6;
        long j7;
        int i6 = this.zzh;
        int i7 = this.zzf;
        if (i7 != i6) {
            byte[] bArr = this.zze;
            int i8 = i6 + 1;
            byte b5 = bArr[i6];
            if (b5 >= 0) {
                this.zzh = i8;
                return b5;
            }
            if (i7 - i8 >= 9) {
                int i9 = i6 + 2;
                int i10 = (bArr[i8] << 7) ^ b5;
                if (i10 < 0) {
                    j4 = i10 ^ (-128);
                } else {
                    int i11 = i6 + 3;
                    int i12 = (bArr[i9] << 14) ^ i10;
                    if (i12 >= 0) {
                        j4 = i12 ^ 16256;
                        i9 = i11;
                    } else {
                        int i13 = i6 + 4;
                        int i14 = i12 ^ (bArr[i11] << 21);
                        if (i14 < 0) {
                            long j8 = (-2080896) ^ i14;
                            i9 = i13;
                            j4 = j8;
                        } else {
                            long j9 = i14;
                            i9 = i6 + 5;
                            long j10 = j9 ^ (((long) bArr[i13]) << 28);
                            if (j10 >= 0) {
                                j7 = 266354560;
                            } else {
                                int i15 = i6 + 6;
                                long j11 = j10 ^ (((long) bArr[i9]) << 35);
                                if (j11 < 0) {
                                    j6 = -34093383808L;
                                } else {
                                    i9 = i6 + 7;
                                    j10 = j11 ^ (((long) bArr[i15]) << 42);
                                    if (j10 >= 0) {
                                        j7 = 4363953127296L;
                                    } else {
                                        i15 = i6 + 8;
                                        j11 = j10 ^ (((long) bArr[i9]) << 49);
                                        if (j11 < 0) {
                                            j6 = -558586000294016L;
                                        } else {
                                            i9 = i6 + 9;
                                            long j12 = (j11 ^ (((long) bArr[i15]) << 56)) ^ 71499008037633920L;
                                            if (j12 < 0) {
                                                int i16 = i6 + 10;
                                                if (bArr[i9] >= 0) {
                                                    i9 = i16;
                                                }
                                            }
                                            j4 = j12;
                                        }
                                    }
                                }
                                j4 = j11 ^ j6;
                                i9 = i15;
                            }
                            j4 = j10 ^ j7;
                        }
                    }
                }
                this.zzh = i9;
                return j4;
            }
        }
        return zzm();
    }

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

    private final byte zzy() throws zzakm {
        if (this.zzh == this.zzf) {
            zzg(1);
        }
        byte[] bArr = this.zze;
        int i6 = this.zzh;
        this.zzh = i6 + 1;
        return bArr[i6];
    }

    private final int zzz() throws zzakm {
        int i6 = this.zzh;
        if (this.zzf - i6 < 4) {
            zzg(4);
            i6 = this.zzh;
        }
        byte[] bArr = this.zze;
        this.zzh = i6 + 4;
        return ((bArr[i6 + 3] & 255) << 24) | (bArr[i6] & 255) | ((bArr[i6 + 1] & 255) << 8) | ((bArr[i6 + 2] & 255) << 16);
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final double zza() {
        return Double.longBitsToDouble(zzab());
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final float zzb() {
        return Float.intBitsToFloat(zzz());
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zzc() {
        return this.zzj + this.zzh;
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zzd() {
        return zzaa();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zze() {
        return zzz();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zzf() {
        return zzaa();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zzg() {
        return zzz();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zzh() {
        return zzaji.zza(zzaa());
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zzi() throws zzakm {
        if (zzw()) {
            this.zzi = 0;
            return 0;
        }
        int iZzaa = zzaa();
        this.zzi = iZzaa;
        if ((iZzaa >>> 3) != 0) {
            return iZzaa;
        }
        throw zzakm.zzc();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zzj() {
        return zzaa();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final long zzk() {
        return zzab();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final long zzl() {
        return zzac();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final long zzm() throws zzakm {
        long j4 = 0;
        for (int i6 = 0; i6 < 64; i6 += 7) {
            byte bZzy = zzy();
            j4 |= ((long) (bZzy & 127)) << i6;
            if ((bZzy & 128) == 0) {
                return j4;
            }
        }
        throw zzakm.zze();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final long zzn() {
        return zzab();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final long zzo() {
        return zzaji.zza(zzac());
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final long zzp() {
        return zzac();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final zzaiw zzq() throws IOException {
        int iZzaa = zzaa();
        int i6 = this.zzf;
        int i7 = this.zzh;
        if (iZzaa <= i6 - i7 && iZzaa > 0) {
            zzaiw zzaiwVarZza = zzaiw.zza(this.zze, i7, iZzaa);
            this.zzh += iZzaa;
            return zzaiwVarZza;
        }
        if (iZzaa == 0) {
            return zzaiw.zza;
        }
        if (iZzaa < 0) {
            throw zzakm.zzf();
        }
        byte[] bArrZzj = zzj(iZzaa);
        if (bArrZzj != null) {
            return zzaiw.zza(bArrZzj);
        }
        int i8 = this.zzh;
        int i9 = this.zzf;
        int length = i9 - i8;
        this.zzj += i9;
        this.zzh = 0;
        this.zzf = 0;
        List<byte[]> listZzf = zzf(iZzaa - length);
        byte[] bArr = new byte[iZzaa];
        System.arraycopy(this.zze, i8, bArr, 0, length);
        for (byte[] bArr2 : listZzf) {
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return zzaiw.zzb(bArr);
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final String zzr() throws zzakm {
        int iZzaa = zzaa();
        if (iZzaa > 0) {
            int i6 = this.zzf;
            int i7 = this.zzh;
            if (iZzaa <= i6 - i7) {
                String str = new String(this.zze, i7, iZzaa, zzaki.zza);
                this.zzh += iZzaa;
                return str;
            }
        }
        if (iZzaa == 0) {
            return "";
        }
        if (iZzaa < 0) {
            throw zzakm.zzf();
        }
        if (iZzaa > this.zzf) {
            return new String(zza(iZzaa, false), zzaki.zza);
        }
        zzg(iZzaa);
        String str2 = new String(this.zze, this.zzh, iZzaa, zzaki.zza);
        this.zzh += iZzaa;
        return str2;
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final String zzs() throws IOException {
        byte[] bArrZza;
        int iZzaa = zzaa();
        int i6 = this.zzh;
        int i7 = this.zzf;
        if (iZzaa <= i7 - i6 && iZzaa > 0) {
            bArrZza = this.zze;
            this.zzh = i6 + iZzaa;
        } else {
            if (iZzaa == 0) {
                return "";
            }
            if (iZzaa < 0) {
                throw zzakm.zzf();
            }
            i6 = 0;
            if (iZzaa <= i7) {
                zzg(iZzaa);
                bArrZza = this.zze;
                this.zzh = iZzaa;
            } else {
                bArrZza = zza(iZzaa, false);
            }
        }
        return zzanb.zzb(bArrZza, i6, iZzaa);
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final boolean zzw() {
        return this.zzh == this.zzf && !zzi(1);
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final boolean zzx() {
        return zzac() != 0;
    }

    private zzajk(InputStream inputStream, int i6) {
        super();
        this.zzk = Integer.MAX_VALUE;
        zzaki.zza(inputStream, "input");
        this.zzd = inputStream;
        this.zze = new byte[4096];
        this.zzf = 0;
        this.zzh = 0;
        this.zzj = 0;
    }

    private static int zza(InputStream inputStream) throws zzakm {
        try {
            return inputStream.available();
        } catch (zzakm e6) {
            e6.zzk();
            throw e6;
        }
    }

    private final List<byte[]> zzf(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.zzd.read(bArr, i7, iMin - i7);
                if (i8 == -1) {
                    throw zzakm.zzj();
                }
                this.zzj += i8;
                i7 += i8;
            }
            i6 -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    private final void zzg(int i6) throws zzakm {
        if (zzi(i6)) {
            return;
        }
        if (i6 <= (this.zzb - this.zzj) - this.zzh) {
            throw zzakm.zzj();
        }
        throw zzakm.zzi();
    }

    private final void zzh(int i6) throws zzakm {
        int i7 = this.zzf;
        int i8 = this.zzh;
        if (i6 <= i7 - i8 && i6 >= 0) {
            this.zzh = i8 + i6;
            return;
        }
        if (i6 < 0) {
            throw zzakm.zzf();
        }
        int i9 = this.zzj;
        int i10 = i9 + i8 + i6;
        int i11 = this.zzk;
        if (i10 > i11) {
            zzh((i11 - i9) - i8);
            throw zzakm.zzj();
        }
        this.zzj = i9 + i8;
        int i12 = i7 - i8;
        this.zzf = 0;
        this.zzh = 0;
        while (i12 < i6) {
            try {
                long j4 = i6 - i12;
                long jZza = zza(this.zzd, j4);
                if (jZza >= 0 && jZza <= j4) {
                    if (jZza == 0) {
                        break;
                    } else {
                        i12 += (int) jZza;
                    }
                } else {
                    throw new IllegalStateException(String.valueOf(this.zzd.getClass()) + "#skip returned invalid result: " + jZza + "\nThe InputStream implementation is buggy.");
                }
            } finally {
                this.zzj += i12;
                zzad();
            }
        }
        if (i12 >= i6) {
            return;
        }
        int i13 = this.zzf;
        int i14 = i13 - this.zzh;
        this.zzh = i13;
        zzg(1);
        while (true) {
            int i15 = i6 - i14;
            int i16 = this.zzf;
            if (i15 <= i16) {
                this.zzh = i15;
                return;
            } else {
                i14 += i16;
                this.zzh = i16;
                zzg(1);
            }
        }
    }

    private final byte[] zzj(int i6) throws zzakm {
        if (i6 == 0) {
            return zzaki.zzb;
        }
        if (i6 < 0) {
            throw zzakm.zzf();
        }
        int i7 = this.zzj;
        int i8 = this.zzh;
        int i9 = i7 + i8 + i6;
        if (i9 - this.zzb > 0) {
            throw zzakm.zzi();
        }
        int i10 = this.zzk;
        if (i9 > i10) {
            zzh((i10 - i7) - i8);
            throw zzakm.zzj();
        }
        int i11 = this.zzf - i8;
        int i12 = i6 - i11;
        if (i12 >= 4096 && i12 > zza(this.zzd)) {
            return null;
        }
        byte[] bArr = new byte[i6];
        System.arraycopy(this.zze, this.zzh, bArr, 0, i11);
        this.zzj += this.zzf;
        this.zzh = 0;
        this.zzf = 0;
        while (i11 < i6) {
            int iZza = zza(this.zzd, bArr, i11, i6 - i11);
            if (iZza == -1) {
                throw zzakm.zzj();
            }
            this.zzj += iZza;
            i11 += iZza;
        }
        return bArr;
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final int zzb(int i6) throws zzakm {
        if (i6 < 0) {
            throw zzakm.zzf();
        }
        int i7 = this.zzj + this.zzh + i6;
        if (i7 < 0) {
            throw zzakm.zzg();
        }
        int i8 = this.zzk;
        if (i7 > i8) {
            throw zzakm.zzj();
        }
        this.zzk = i7;
        zzad();
        return i8;
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final void zzc(int i6) throws zzakm {
        if (this.zzi != i6) {
            throw zzakm.zzb();
        }
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final void zzd(int i6) {
        this.zzk = i6;
        zzad();
    }

    @Override // com.google.android.gms.internal.p002firebaseauthapi.zzaji
    public final boolean zze(int i6) throws zzakm {
        int i7 = i6 & 7;
        int i8 = 0;
        if (i7 == 0) {
            if (this.zzf - this.zzh < 10) {
                while (i8 < 10) {
                    if (zzy() < 0) {
                        i8++;
                    }
                }
                throw zzakm.zze();
            }
            while (i8 < 10) {
                byte[] bArr = this.zze;
                int i9 = this.zzh;
                this.zzh = i9 + 1;
                if (bArr[i9] < 0) {
                    i8++;
                }
            }
            throw zzakm.zze();
            return true;
        }
        if (i7 == 1) {
            zzh(8);
            return true;
        }
        if (i7 == 2) {
            zzh(zzaa());
            return true;
        }
        if (i7 == 3) {
            zzv();
            zzc(((i6 >>> 3) << 3) | 4);
            return true;
        }
        if (i7 == 4) {
            zzu();
            return false;
        }
        if (i7 != 5) {
            throw zzakm.zza();
        }
        zzh(4);
        return true;
    }

    private static int zza(InputStream inputStream, byte[] bArr, int i6, int i7) throws zzakm {
        try {
            return inputStream.read(bArr, i6, i7);
        } catch (zzakm e6) {
            e6.zzk();
            throw e6;
        }
    }

    private final boolean zzi(int i6) throws zzakm {
        do {
            int i7 = this.zzh;
            int i8 = i7 + i6;
            int i9 = this.zzf;
            if (i8 > i9) {
                int i10 = this.zzb;
                int i11 = this.zzj;
                if (i6 > (i10 - i11) - i7 || i11 + i7 + i6 > this.zzk) {
                    return false;
                }
                if (i7 > 0) {
                    if (i9 > i7) {
                        byte[] bArr = this.zze;
                        System.arraycopy(bArr, i7, bArr, 0, i9 - i7);
                    }
                    this.zzj += i7;
                    this.zzf -= i7;
                    this.zzh = 0;
                }
                InputStream inputStream = this.zzd;
                byte[] bArr2 = this.zze;
                int i12 = this.zzf;
                int iZza = zza(inputStream, bArr2, i12, Math.min(bArr2.length - i12, (this.zzb - this.zzj) - i12));
                if (iZza == 0 || iZza < -1 || iZza > this.zze.length) {
                    throw new IllegalStateException(String.valueOf(this.zzd.getClass()) + "#read(byte[]) returned invalid result: " + iZza + "\nThe InputStream implementation is buggy.");
                }
                if (iZza <= 0) {
                    return false;
                }
                this.zzf += iZza;
                zzad();
            } else {
                throw new IllegalStateException(o.j(i6, "refillBuffer() called when ", " bytes were already available in buffer"));
            }
        } while (this.zzf < i6);
        return true;
    }

    private static long zza(InputStream inputStream, long j4) throws zzakm {
        try {
            return inputStream.skip(j4);
        } catch (zzakm e6) {
            e6.zzk();
            throw e6;
        }
    }

    private final byte[] zza(int i6, boolean z6) throws IOException {
        byte[] bArrZzj = zzj(i6);
        if (bArrZzj != null) {
            return bArrZzj;
        }
        int i7 = this.zzh;
        int i8 = this.zzf;
        int length = i8 - i7;
        this.zzj += i8;
        this.zzh = 0;
        this.zzf = 0;
        List<byte[]> listZzf = zzf(i6 - length);
        byte[] bArr = new byte[i6];
        System.arraycopy(this.zze, i7, bArr, 0, length);
        for (byte[] bArr2 : listZzf) {
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }
}
