package o;

import com.google.android.gms.common.api.Api;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.ti, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2547ti extends AbstractC2071oh {
    public final FileInputStream n;

    /* renamed from: o, reason: collision with root package name */
    public final byte[] f733o;
    public int p;
    public int q;
    public int r;
    public int s;
    public int t;
    public int u = Api.BaseClientBuilder.API_PRIORITY_OTHER;

    public C2547ti(FileInputStream fileInputStream) {
        Charset charset = AbstractC2418sI.a;
        this.n = fileInputStream;
        this.f733o = new byte[4096];
        this.p = 0;
        this.r = 0;
        this.t = 0;
    }

    @Override // o.AbstractC2071oh
    public final int A() {
        return J();
    }

    @Override // o.AbstractC2071oh
    public final long B() {
        return K();
    }

    @Override // o.AbstractC2071oh
    public final boolean C(int i) throws EI {
        int i2 = i & 7;
        int i3 = 0;
        if (i2 != 0) {
            if (i2 == 1) {
                O(8);
                return true;
            }
            if (i2 == 2) {
                O(J());
                return true;
            }
            if (i2 == 3) {
                D();
                a(((i >>> 3) << 3) | 4);
                return true;
            }
            if (i2 == 4) {
                return false;
            }
            if (i2 != 5) {
                throw EI.b();
            }
            O(4);
            return true;
        }
        int i4 = this.p - this.r;
        byte[] bArr = this.f733o;
        if (i4 >= 10) {
            while (i3 < 10) {
                int i5 = this.r;
                this.r = i5 + 1;
                if (bArr[i5] < 0) {
                    i3++;
                }
            }
            throw EI.c();
        }
        while (i3 < 10) {
            if (this.r == this.p) {
                N(1);
            }
            int i6 = this.r;
            this.r = i6 + 1;
            if (bArr[i6] < 0) {
                i3++;
            }
        }
        throw EI.c();
        return true;
    }

    public final byte[] E(int i) throws IOException {
        byte[] bArrF = F(i);
        if (bArrF != null) {
            return bArrF;
        }
        int i2 = this.r;
        int i3 = this.p;
        int length = i3 - i2;
        this.t += i3;
        this.r = 0;
        this.p = 0;
        ArrayList arrayListG = G(i - length);
        byte[] bArr = new byte[i];
        System.arraycopy(this.f733o, i2, bArr, 0, length);
        int size = arrayListG.size();
        int i4 = 0;
        while (i4 < size) {
            Object obj = arrayListG.get(i4);
            i4++;
            byte[] bArr2 = (byte[]) obj;
            System.arraycopy(bArr2, 0, bArr, length, bArr2.length);
            length += bArr2.length;
        }
        return bArr;
    }

    public final byte[] F(int i) throws IOException {
        if (i == 0) {
            return AbstractC2418sI.b;
        }
        if (i < 0) {
            throw EI.d();
        }
        int i2 = this.t;
        int i3 = this.r;
        int i4 = i2 + i3 + i;
        if (i4 - Api.BaseClientBuilder.API_PRIORITY_OTHER > 0) {
            throw new EI("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
        }
        int i5 = this.u;
        if (i4 > i5) {
            O((i5 - i2) - i3);
            throw EI.e();
        }
        int i6 = this.p - i3;
        int i7 = i - i6;
        FileInputStream fileInputStream = this.n;
        if (i7 >= 4096) {
            try {
                if (i7 > fileInputStream.available()) {
                    return null;
                }
            } catch (EI e) {
                e.l = true;
                throw e;
            }
        }
        byte[] bArr = new byte[i];
        System.arraycopy(this.f733o, this.r, bArr, 0, i6);
        this.t += this.p;
        this.r = 0;
        this.p = 0;
        while (i6 < i) {
            try {
                int i8 = fileInputStream.read(bArr, i6, i - i6);
                if (i8 == -1) {
                    throw EI.e();
                }
                this.t += i8;
                i6 += i8;
            } catch (EI e2) {
                e2.l = true;
                throw e2;
            }
        }
        return bArr;
    }

    public final ArrayList G(int i) throws IOException {
        ArrayList arrayList = new ArrayList();
        while (i > 0) {
            int iMin = Math.min(i, 4096);
            byte[] bArr = new byte[iMin];
            int i2 = 0;
            while (i2 < iMin) {
                int i3 = this.n.read(bArr, i2, iMin - i2);
                if (i3 == -1) {
                    throw EI.e();
                }
                this.t += i3;
                i2 += i3;
            }
            i -= iMin;
            arrayList.add(bArr);
        }
        return arrayList;
    }

    public final int H() throws EI {
        int i = this.r;
        if (this.p - i < 4) {
            N(4);
            i = this.r;
        }
        this.r = i + 4;
        byte[] bArr = this.f733o;
        return ((bArr[i + 3] & 255) << 24) | (bArr[i] & 255) | ((bArr[i + 1] & 255) << 8) | ((bArr[i + 2] & 255) << 16);
    }

    public final long I() throws EI {
        int i = this.r;
        if (this.p - i < 8) {
            N(8);
            i = this.r;
        }
        this.r = i + 8;
        byte[] bArr = this.f733o;
        return ((bArr[i + 7] & 255) << 56) | (bArr[i] & 255) | ((bArr[i + 1] & 255) << 8) | ((bArr[i + 2] & 255) << 16) | ((bArr[i + 3] & 255) << 24) | ((bArr[i + 4] & 255) << 32) | ((bArr[i + 5] & 255) << 40) | ((bArr[i + 6] & 255) << 48);
    }

    public final int J() {
        int i;
        int i2 = this.r;
        int i3 = this.p;
        if (i3 != i2) {
            int i4 = i2 + 1;
            byte[] bArr = this.f733o;
            byte b = bArr[i2];
            if (b >= 0) {
                this.r = i4;
                return b;
            }
            if (i3 - i4 >= 9) {
                int i5 = i2 + 2;
                int i6 = (bArr[i4] << 7) ^ b;
                if (i6 < 0) {
                    i = i6 ^ (-128);
                } else {
                    int i7 = i2 + 3;
                    int i8 = (bArr[i5] << 14) ^ i6;
                    if (i8 >= 0) {
                        i = i8 ^ 16256;
                    } else {
                        int i9 = i2 + 4;
                        int i10 = i8 ^ (bArr[i7] << 21);
                        if (i10 < 0) {
                            i = (-2080896) ^ i10;
                        } else {
                            i7 = i2 + 5;
                            byte b2 = bArr[i9];
                            int i11 = (i10 ^ (b2 << 28)) ^ 266354560;
                            if (b2 < 0) {
                                i9 = i2 + 6;
                                if (bArr[i7] < 0) {
                                    i7 = i2 + 7;
                                    if (bArr[i9] < 0) {
                                        i9 = i2 + 8;
                                        if (bArr[i7] < 0) {
                                            i7 = i2 + 9;
                                            if (bArr[i9] < 0) {
                                                int i12 = i2 + 10;
                                                if (bArr[i7] >= 0) {
                                                    i5 = i12;
                                                    i = i11;
                                                }
                                            }
                                        }
                                    }
                                }
                                i = i11;
                            }
                            i = i11;
                        }
                        i5 = i9;
                    }
                    i5 = i7;
                }
                this.r = i5;
                return i;
            }
        }
        return (int) L();
    }

    public final long K() {
        long j;
        long j2;
        long j3;
        long j4;
        int i = this.r;
        int i2 = this.p;
        if (i2 != i) {
            int i3 = i + 1;
            byte[] bArr = this.f733o;
            byte b = bArr[i];
            if (b >= 0) {
                this.r = i3;
                return b;
            }
            if (i2 - i3 >= 9) {
                int i4 = i + 2;
                int i5 = (bArr[i3] << 7) ^ b;
                if (i5 < 0) {
                    j = i5 ^ (-128);
                } else {
                    int i6 = i + 3;
                    int i7 = (bArr[i4] << 14) ^ i5;
                    if (i7 >= 0) {
                        j = i7 ^ 16256;
                        i4 = i6;
                    } else {
                        int i8 = i + 4;
                        int i9 = i7 ^ (bArr[i6] << 21);
                        if (i9 < 0) {
                            j4 = (-2080896) ^ i9;
                        } else {
                            long j5 = i9;
                            i4 = i + 5;
                            long j6 = j5 ^ (bArr[i8] << 28);
                            if (j6 >= 0) {
                                j3 = 266354560;
                            } else {
                                i8 = i + 6;
                                long j7 = j6 ^ (bArr[i4] << 35);
                                if (j7 < 0) {
                                    j2 = -34093383808L;
                                } else {
                                    i4 = i + 7;
                                    j6 = j7 ^ (bArr[i8] << 42);
                                    if (j6 >= 0) {
                                        j3 = 4363953127296L;
                                    } else {
                                        i8 = i + 8;
                                        j7 = j6 ^ (bArr[i4] << 49);
                                        if (j7 < 0) {
                                            j2 = -558586000294016L;
                                        } else {
                                            i4 = i + 9;
                                            long j8 = (j7 ^ (bArr[i8] << 56)) ^ 71499008037633920L;
                                            if (j8 < 0) {
                                                int i10 = i + 10;
                                                if (bArr[i4] >= 0) {
                                                    i4 = i10;
                                                }
                                            }
                                            j = j8;
                                        }
                                    }
                                }
                                j4 = j2 ^ j7;
                            }
                            j = j3 ^ j6;
                        }
                        i4 = i8;
                        j = j4;
                    }
                }
                this.r = i4;
                return j;
            }
        }
        return L();
    }

    public final long L() throws EI {
        long j = 0;
        for (int i = 0; i < 64; i += 7) {
            if (this.r == this.p) {
                N(1);
            }
            int i2 = this.r;
            this.r = i2 + 1;
            j |= (r3 & Byte.MAX_VALUE) << i;
            if ((this.f733o[i2] & 128) == 0) {
                return j;
            }
        }
        throw EI.c();
    }

    public final void M() {
        int i = this.p + this.q;
        this.p = i;
        int i2 = this.t + i;
        int i3 = this.u;
        if (i2 <= i3) {
            this.q = 0;
            return;
        }
        int i4 = i2 - i3;
        this.q = i4;
        this.p = i - i4;
    }

    public final void N(int i) throws EI {
        if (P(i)) {
            return;
        }
        if (i <= (Api.BaseClientBuilder.API_PRIORITY_OTHER - this.t) - this.r) {
            throw EI.e();
        }
        throw new EI("Protocol message was too large.  May be malicious.  Use CodedInputStream.setSizeLimit() to increase the size limit.");
    }

    public final void O(int i) throws EI {
        int i2 = this.p;
        int i3 = this.r;
        int i4 = i2 - i3;
        if (i <= i4 && i >= 0) {
            this.r = i3 + i;
            return;
        }
        FileInputStream fileInputStream = this.n;
        if (i < 0) {
            throw EI.d();
        }
        int i5 = this.t;
        int i6 = i5 + i3;
        int i7 = i6 + i;
        int i8 = this.u;
        if (i7 > i8) {
            O((i8 - i5) - i3);
            throw EI.e();
        }
        this.t = i6;
        this.p = 0;
        this.r = 0;
        while (i4 < i) {
            long j = i - i4;
            try {
                try {
                    long jSkip = fileInputStream.skip(j);
                    if (jSkip < 0 || jSkip > j) {
                        throw new IllegalStateException(fileInputStream.getClass() + "#skip returned invalid result: " + jSkip + "\nThe InputStream implementation is buggy.");
                    }
                    if (jSkip == 0) {
                        break;
                    } else {
                        i4 += (int) jSkip;
                    }
                } catch (EI e) {
                    e.l = true;
                    throw e;
                }
            } catch (Throwable th) {
                this.t += i4;
                M();
                throw th;
            }
        }
        this.t += i4;
        M();
        if (i4 >= i) {
            return;
        }
        int i9 = this.p;
        int i10 = i9 - this.r;
        this.r = i9;
        N(1);
        while (true) {
            int i11 = i - i10;
            int i12 = this.p;
            if (i11 <= i12) {
                this.r = i11;
                return;
            } else {
                i10 += i12;
                this.r = i12;
                N(1);
            }
        }
    }

    public final boolean P(int i) throws IOException {
        FileInputStream fileInputStream = this.n;
        int i2 = this.r;
        int i3 = i2 + i;
        int i4 = this.p;
        if (i3 <= i4) {
            throw new IllegalStateException(AbstractC0126Ew.k(i, "refillBuffer() called when ", " bytes were already available in buffer"));
        }
        int i5 = this.t;
        if (i <= (Api.BaseClientBuilder.API_PRIORITY_OTHER - i5) - i2 && i5 + i2 + i <= this.u) {
            byte[] bArr = this.f733o;
            if (i2 > 0) {
                if (i4 > i2) {
                    System.arraycopy(bArr, i2, bArr, 0, i4 - i2);
                }
                this.t += i2;
                this.p -= i2;
                this.r = 0;
            }
            int i6 = this.p;
            try {
                int i7 = fileInputStream.read(bArr, i6, Math.min(bArr.length - i6, (Api.BaseClientBuilder.API_PRIORITY_OTHER - this.t) - i6));
                if (i7 == 0 || i7 < -1 || i7 > bArr.length) {
                    throw new IllegalStateException(fileInputStream.getClass() + "#read(byte[]) returned invalid result: " + i7 + "\nThe InputStream implementation is buggy.");
                }
                if (i7 > 0) {
                    this.p += i7;
                    M();
                    if (this.p >= i) {
                        return true;
                    }
                    return P(i);
                }
            } catch (EI e) {
                e.l = true;
                throw e;
            }
        }
        return false;
    }

    @Override // o.AbstractC2071oh
    public final void a(int i) throws EI {
        if (this.s != i) {
            throw new EI("Protocol message end-group tag did not match expected tag.");
        }
    }

    @Override // o.AbstractC2071oh
    public final int b() {
        return this.t + this.r;
    }

    @Override // o.AbstractC2071oh
    public final boolean c() {
        return this.r == this.p && !P(1);
    }

    @Override // o.AbstractC2071oh
    public final void h(int i) {
        this.u = i;
        M();
    }

    @Override // o.AbstractC2071oh
    public final int j(int i) throws EI {
        if (i < 0) {
            throw EI.d();
        }
        int i2 = this.t + this.r + i;
        if (i2 < 0) {
            throw new EI("Failed to parse the message.");
        }
        int i3 = this.u;
        if (i2 > i3) {
            throw EI.e();
        }
        this.u = i2;
        M();
        return i3;
    }

    @Override // o.AbstractC2071oh
    public final boolean k() {
        return K() != 0;
    }

    @Override // o.AbstractC2071oh
    public final C0109Ef l() throws IOException {
        int iJ = J();
        int i = this.p;
        int i2 = this.r;
        int i3 = i - i2;
        byte[] bArr = this.f733o;
        if (iJ <= i3 && iJ > 0) {
            C0109Ef c0109EfC = AbstractC0161Gf.c(bArr, i2, iJ);
            this.r += iJ;
            return c0109EfC;
        }
        if (iJ == 0) {
            return AbstractC0161Gf.m;
        }
        if (iJ < 0) {
            throw EI.d();
        }
        byte[] bArrF = F(iJ);
        if (bArrF != null) {
            return AbstractC0161Gf.c(bArrF, 0, bArrF.length);
        }
        int i4 = this.r;
        int i5 = this.p;
        int length = i5 - i4;
        this.t += i5;
        this.r = 0;
        this.p = 0;
        ArrayList arrayListG = G(iJ - length);
        byte[] bArr2 = new byte[iJ];
        System.arraycopy(bArr, i4, bArr2, 0, length);
        int size = arrayListG.size();
        int i6 = 0;
        while (i6 < size) {
            Object obj = arrayListG.get(i6);
            i6++;
            byte[] bArr3 = (byte[]) obj;
            System.arraycopy(bArr3, 0, bArr2, length, bArr3.length);
            length += bArr3.length;
        }
        C0109Ef c0109Ef = AbstractC0161Gf.m;
        return new C0109Ef(bArr2);
    }

    @Override // o.AbstractC2071oh
    public final double m() {
        return Double.longBitsToDouble(I());
    }

    @Override // o.AbstractC2071oh
    public final int n() {
        return J();
    }

    @Override // o.AbstractC2071oh
    public final int o() {
        return H();
    }

    @Override // o.AbstractC2071oh
    public final long p() {
        return I();
    }

    @Override // o.AbstractC2071oh
    public final float q() {
        return Float.intBitsToFloat(H());
    }

    @Override // o.AbstractC2071oh
    public final int r() {
        return J();
    }

    @Override // o.AbstractC2071oh
    public final long s() {
        return K();
    }

    @Override // o.AbstractC2071oh
    public final int t() {
        return H();
    }

    @Override // o.AbstractC2071oh
    public final long u() {
        return I();
    }

    @Override // o.AbstractC2071oh
    public final int v() {
        int iJ = J();
        return (-(iJ & 1)) ^ (iJ >>> 1);
    }

    @Override // o.AbstractC2071oh
    public final long w() {
        long jK = K();
        return (-(jK & 1)) ^ (jK >>> 1);
    }

    @Override // o.AbstractC2071oh
    public final String x() throws EI {
        int iJ = J();
        byte[] bArr = this.f733o;
        if (iJ > 0) {
            int i = this.p;
            int i2 = this.r;
            if (iJ <= i - i2) {
                String str = new String(bArr, i2, iJ, AbstractC2418sI.a);
                this.r += iJ;
                return str;
            }
        }
        if (iJ == 0) {
            return "";
        }
        if (iJ < 0) {
            throw EI.d();
        }
        if (iJ > this.p) {
            return new String(E(iJ), AbstractC2418sI.a);
        }
        N(iJ);
        String str2 = new String(bArr, this.r, iJ, AbstractC2418sI.a);
        this.r += iJ;
        return str2;
    }

    @Override // o.AbstractC2071oh
    public final String y() throws IOException {
        int iJ = J();
        int i = this.r;
        int i2 = this.p;
        int i3 = i2 - i;
        byte[] bArrE = this.f733o;
        if (iJ <= i3 && iJ > 0) {
            this.r = i + iJ;
        } else {
            if (iJ == 0) {
                return "";
            }
            if (iJ < 0) {
                throw EI.d();
            }
            i = 0;
            if (iJ <= i2) {
                N(iJ);
                this.r = iJ;
            } else {
                bArrE = E(iJ);
            }
        }
        return AbstractC2074oi0.a.u(bArrE, i, iJ);
    }

    @Override // o.AbstractC2071oh
    public final int z() throws EI {
        if (c()) {
            this.s = 0;
            return 0;
        }
        int iJ = J();
        this.s = iJ;
        if ((iJ >>> 3) != 0) {
            return iJ;
        }
        throw new EI("Protocol message contained an invalid tag (zero).");
    }
}
