package com.squareup.wire;

import X.PEG;
import X.Q1J;
import com.bytedance.covode.number.Covode;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;

public final class ProtoReader {
    public final PEG LIZ;
    public long LIZIZ;
    public long LIZJ = Long.MAX_VALUE;
    public int LIZLLL;
    public int LJ = 2;
    public int LJFF = -1;
    public long LJI = -1;
    public FieldEncoding LJII;

    static {
        Covode.recordClassIndex(47732);
    }

    public final FieldEncoding peekFieldEncoding() {
        return this.LJII;
    }

    public final Q1J readBytes() {
        long LIZIZ2 = LIZIZ();
        this.LIZ.LIZLLL(LIZIZ2);
        return this.LIZ.LJI(LIZIZ2);
    }

    public final String readString() {
        long LIZIZ2 = LIZIZ();
        this.LIZ.LIZLLL(LIZIZ2);
        return this.LIZ.LJII(LIZIZ2);
    }

    private long LIZIZ() {
        if (this.LJ == 2) {
            long j = this.LIZJ - this.LIZIZ;
            this.LIZ.LIZLLL(j);
            this.LJ = 6;
            this.LIZIZ = this.LIZJ;
            this.LIZJ = this.LJI;
            this.LJI = -1;
            return j;
        }
        throw new ProtocolException("Expected LENGTH_DELIMITED but was " + this.LJ);
    }

    public final long beginMessage() {
        if (this.LJ == 2) {
            int i = this.LIZLLL + 1;
            this.LIZLLL = i;
            if (i <= 65) {
                long j = this.LJI;
                this.LJI = -1;
                this.LJ = 6;
                return j;
            }
            throw new IOException("Wire recursion limit exceeded");
        }
        throw new IllegalStateException("Unexpected call to beginMessage()");
    }

    public final int readFixed32() {
        int i = this.LJ;
        if (i == 5 || i == 2) {
            this.LIZ.LIZLLL(4);
            this.LIZIZ += 4;
            int LJIIL = this.LIZ.LJIIL();
            LIZIZ(5);
            return LJIIL;
        }
        throw new ProtocolException("Expected FIXED32 or LENGTH_DELIMITED but was " + this.LJ);
    }

    public final long readFixed64() {
        int i = this.LJ;
        if (i == 1 || i == 2) {
            this.LIZ.LIZLLL(8);
            this.LIZIZ += 8;
            long LJIILIIL = this.LIZ.LJIILIIL();
            LIZIZ(1);
            return LJIILIIL;
        }
        throw new ProtocolException("Expected FIXED64 or LENGTH_DELIMITED but was " + this.LJ);
    }

    public final int readVarint32() {
        int i = this.LJ;
        if (i == 0 || i == 2) {
            int LIZ2 = LIZ();
            LIZIZ(0);
            return LIZ2;
        }
        throw new ProtocolException("Expected VARINT or LENGTH_DELIMITED but was " + this.LJ);
    }

    public final void skip() {
        int i = this.LJ;
        if (i == 0) {
            readVarint64();
        } else if (i == 1) {
            readFixed64();
        } else if (i == 2) {
            this.LIZ.LJIIJJI(LIZIZ());
        } else if (i == 5) {
            readFixed32();
        } else {
            throw new IllegalStateException("Unexpected call to skip()");
        }
    }

    public final long readVarint64() {
        int i = this.LJ;
        if (i == 0 || i == 2) {
            long j = 0;
            int i2 = 0;
            do {
                this.LIZ.LIZLLL(1);
                this.LIZIZ++;
                byte LJII2 = this.LIZ.LJII();
                j |= ((long) (LJII2 & Byte.MAX_VALUE)) << i2;
                if ((LJII2 & 128) == 0) {
                    LIZIZ(0);
                    return j;
                }
                i2 += 7;
            } while (i2 < 64);
            throw new ProtocolException("WireInput encountered a malformed varint");
        }
        throw new ProtocolException("Expected VARINT or LENGTH_DELIMITED but was " + this.LJ);
    }

    private int LIZ() {
        int i;
        this.LIZ.LIZLLL(1);
        this.LIZIZ++;
        byte LJII2 = this.LIZ.LJII();
        if (LJII2 >= 0) {
            return LJII2;
        }
        byte b = LJII2 & Byte.MAX_VALUE;
        this.LIZ.LIZLLL(1);
        this.LIZIZ++;
        byte LJII3 = this.LIZ.LJII();
        if (LJII3 >= 0) {
            i = LJII3 << 7;
        } else {
            b |= (LJII3 & Byte.MAX_VALUE) << 7;
            this.LIZ.LIZLLL(1);
            this.LIZIZ++;
            byte LJII4 = this.LIZ.LJII();
            if (LJII4 >= 0) {
                i = LJII4 << 14;
            } else {
                b |= (LJII4 & Byte.MAX_VALUE) << 14;
                this.LIZ.LIZLLL(1);
                this.LIZIZ++;
                byte LJII5 = this.LIZ.LJII();
                if (LJII5 >= 0) {
                    i = LJII5 << 21;
                } else {
                    this.LIZ.LIZLLL(1);
                    this.LIZIZ++;
                    byte LJII6 = this.LIZ.LJII();
                    byte b2 = b | ((LJII5 & Byte.MAX_VALUE) << 21) | (LJII6 << 28);
                    if (LJII6 >= 0) {
                        return b2;
                    }
                    int i2 = 0;
                    do {
                        this.LIZ.LIZLLL(1);
                        this.LIZIZ++;
                        if (this.LIZ.LJII() >= 0) {
                            return b2;
                        }
                        i2++;
                    } while (i2 < 5);
                    throw new ProtocolException("Malformed VARINT");
                }
            }
        }
        return b | i;
    }

    public final int nextTag() {
        int i = this.LJ;
        if (i == 7) {
            this.LJ = 2;
            return this.LJFF;
        } else if (i == 6) {
            while (this.LIZIZ < this.LIZJ && !this.LIZ.LJ()) {
                int LIZ2 = LIZ();
                if (LIZ2 != 0) {
                    int i2 = LIZ2 >> 3;
                    this.LJFF = i2;
                    int i3 = LIZ2 & 7;
                    if (i3 == 0) {
                        this.LJII = FieldEncoding.VARINT;
                        this.LJ = 0;
                        return this.LJFF;
                    } else if (i3 == 1) {
                        this.LJII = FieldEncoding.FIXED64;
                        this.LJ = 1;
                        return this.LJFF;
                    } else if (i3 == 2) {
                        this.LJII = FieldEncoding.LENGTH_DELIMITED;
                        this.LJ = 2;
                        int LIZ3 = LIZ();
                        if (LIZ3 < 0) {
                            throw new ProtocolException("Negative length: ".concat(String.valueOf(LIZ3)));
                        } else if (this.LJI == -1) {
                            long j = this.LIZJ;
                            this.LJI = j;
                            long j2 = this.LIZIZ + ((long) LIZ3);
                            this.LIZJ = j2;
                            if (j2 <= j) {
                                return this.LJFF;
                            }
                            throw new EOFException();
                        } else {
                            throw new IllegalStateException();
                        }
                    } else if (i3 == 3) {
                        LIZ(i2);
                    } else if (i3 == 4) {
                        throw new ProtocolException("Unexpected end group");
                    } else if (i3 == 5) {
                        this.LJII = FieldEncoding.FIXED32;
                        this.LJ = 5;
                        return this.LJFF;
                    } else {
                        throw new ProtocolException("Unexpected field encoding: ".concat(String.valueOf(i3)));
                    }
                } else {
                    throw new ProtocolException("Unexpected tag 0");
                }
            }
            return -1;
        } else {
            throw new IllegalStateException("Unexpected call to nextTag()");
        }
    }

    public ProtoReader(PEG peg) {
        this.LIZ = peg;
    }

    private void LIZ(int i) {
        while (this.LIZIZ < this.LIZJ && !this.LIZ.LJ()) {
            int LIZ2 = LIZ();
            if (LIZ2 != 0) {
                int i2 = LIZ2 >> 3;
                int i3 = LIZ2 & 7;
                if (i3 == 0) {
                    this.LJ = 0;
                    readVarint64();
                } else if (i3 == 1) {
                    this.LJ = 1;
                    readFixed64();
                } else if (i3 == 2) {
                    long LIZ3 = (long) LIZ();
                    this.LIZIZ += LIZ3;
                    this.LIZ.LJIIJJI(LIZ3);
                } else if (i3 == 3) {
                    LIZ(i2);
                } else if (i3 != 4) {
                    if (i3 == 5) {
                        this.LJ = 5;
                        readFixed32();
                    } else {
                        throw new ProtocolException("Unexpected field encoding: ".concat(String.valueOf(i3)));
                    }
                } else if (i2 != i) {
                    throw new ProtocolException("Unexpected end group");
                } else {
                    return;
                }
            } else {
                throw new ProtocolException("Unexpected tag 0");
            }
        }
        throw new EOFException();
    }

    private void LIZIZ(int i) {
        if (this.LJ == i) {
            this.LJ = 6;
            return;
        }
        long j = this.LIZIZ;
        long j2 = this.LIZJ;
        if (j > j2) {
            throw new IOException("Expected to end at " + this.LIZJ + " but was " + this.LIZIZ);
        } else if (j == j2) {
            this.LIZJ = this.LJI;
            this.LJI = -1;
            this.LJ = 6;
        } else {
            this.LJ = 7;
        }
    }

    public final void endMessage(long j) {
        if (this.LJ == 6) {
            int i = this.LIZLLL - 1;
            this.LIZLLL = i;
            if (i < 0 || this.LJI != -1) {
                throw new IllegalStateException("No corresponding call to beginMessage()");
            } else if (this.LIZIZ == this.LIZJ || i == 0) {
                this.LIZJ = j;
            } else {
                throw new IOException("Expected to end at " + this.LIZJ + " but was " + this.LIZIZ);
            }
        } else {
            throw new IllegalStateException("Unexpected call to endMessage()");
        }
    }
}
