package com.google.crypto.tink.shaded.protobuf;

import com.google.crypto.tink.shaded.protobuf.ByteString;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes2.dex */
public final class n extends nb.g {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Logger f23944f = Logger.getLogger(n.class.getName());

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final boolean f23945g = l1.f23936e;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public xyz.stream.utils.j0 f23946b;

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

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

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

    public n(byte[] bArr, int i10) {
        if (((bArr.length - i10) | i10) < 0) {
            throw new IllegalArgumentException(String.format(Locale.US, "Array range is invalid. Buffer.length=%d, offset=%d, length=%d", Integer.valueOf(bArr.length), 0, Integer.valueOf(i10)));
        }
        this.f23947c = bArr;
        this.f23949e = 0;
        this.f23948d = i10;
    }

    public static int W(int i10) {
        return o0(i10) + 1;
    }

    public static int X(int i10, ByteString byteString) {
        int iO0 = o0(i10);
        int size = byteString.size();
        return q0(size) + size + iO0;
    }

    public static int Y(int i10) {
        return o0(i10) + 8;
    }

    public static int Z(int i10, int i11) {
        return s0(i11) + o0(i10);
    }

    public static int a0(int i10) {
        return o0(i10) + 4;
    }

    public static int b0(int i10) {
        return o0(i10) + 8;
    }

    public static int c0(int i10) {
        return o0(i10) + 4;
    }

    public static int d0(int i10, o0 o0Var, a1 a1Var) {
        return ((b) o0Var).c(a1Var) + (o0(i10) * 2);
    }

    public static int e0(int i10, int i11) {
        return s0(i11) + o0(i10);
    }

    public static int f0(int i10, long j10) {
        return s0(j10) + o0(i10);
    }

    public static int g0(int i10) {
        return o0(i10) + 4;
    }

    public static int h0(int i10) {
        return o0(i10) + 8;
    }

    public static int i0(int i10, int i11) {
        return j0(i11) + o0(i10);
    }

    public static int j0(int i10) {
        return q0((i10 >> 31) ^ (i10 << 1));
    }

    public static int k0(int i10, long j10) {
        return l0(j10) + o0(i10);
    }

    public static int l0(long j10) {
        return s0((j10 >> 63) ^ (j10 << 1));
    }

    public static int m0(int i10, String str) {
        return n0(str) + o0(i10);
    }

    public static int n0(String str) {
        int length;
        try {
            length = n1.b(str);
        } catch (Utf8$UnpairedSurrogateException unused) {
            length = str.getBytes(b0.f23882a).length;
        }
        return q0(length) + length;
    }

    public static int o0(int i10) {
        return q0(i10 << 3);
    }

    public static int p0(int i10, int i11) {
        return q0(i11) + o0(i10);
    }

    public static int q0(int i10) {
        return (352 - (Integer.numberOfLeadingZeros(i10) * 9)) >>> 6;
    }

    public static int r0(int i10, long j10) {
        return s0(j10) + o0(i10);
    }

    public static int s0(long j10) {
        return (640 - (Long.numberOfLeadingZeros(j10) * 9)) >>> 6;
    }

    public final void A0(int i10, int i11) {
        D0(i10, 0);
        B0(i11);
    }

    public final void B0(int i10) {
        if (i10 >= 0) {
            F0(i10);
        } else {
            H0(i10);
        }
    }

    public final void C0(String str) {
        int i10 = this.f23949e;
        try {
            int iQ0 = q0(str.length() * 3);
            int iQ02 = q0(str.length());
            int i11 = this.f23948d;
            byte[] bArr = this.f23947c;
            if (iQ02 == iQ0) {
                int i12 = i10 + iQ02;
                this.f23949e = i12;
                int iX = n1.f23950a.X(str, bArr, i12, i11 - i12);
                this.f23949e = i10;
                F0((iX - i10) - iQ02);
                this.f23949e = iX;
            } else {
                F0(n1.b(str));
                int i13 = this.f23949e;
                this.f23949e = n1.f23950a.X(str, bArr, i13, i11 - i13);
            }
        } catch (Utf8$UnpairedSurrogateException e10) {
            this.f23949e = i10;
            f23944f.log(Level.WARNING, "Converting ill-formed UTF-16. Your Protocol Buffer will not round trip correctly!", (Throwable) e10);
            byte[] bytes = str.getBytes(b0.f23882a);
            try {
                F0(bytes.length);
                u0(bytes, 0, bytes.length);
            } catch (IndexOutOfBoundsException e11) {
                throw new CodedOutputStream$OutOfSpaceException(e11);
            }
        } catch (IndexOutOfBoundsException e12) {
            throw new CodedOutputStream$OutOfSpaceException(e12);
        }
    }

    public final void D0(int i10, int i11) {
        F0((i10 << 3) | i11);
    }

    public final void E0(int i10, int i11) {
        D0(i10, 0);
        F0(i11);
    }

    public final void F0(int i10) {
        int i11;
        int i12 = this.f23949e;
        while (true) {
            int i13 = i10 & (-128);
            byte[] bArr = this.f23947c;
            if (i13 == 0) {
                i11 = i12 + 1;
                bArr[i12] = (byte) i10;
                this.f23949e = i11;
                return;
            } else {
                i11 = i12 + 1;
                try {
                    bArr[i12] = (byte) (i10 | 128);
                    i10 >>>= 7;
                    i12 = i11;
                } catch (IndexOutOfBoundsException e10) {
                    throw new CodedOutputStream$OutOfSpaceException(i11, this.f23948d, 1, e10);
                }
            }
            throw new CodedOutputStream$OutOfSpaceException(i11, this.f23948d, 1, e10);
        }
    }

    public final void G0(int i10, long j10) {
        D0(i10, 0);
        H0(j10);
    }

    public final void H0(long j10) {
        int i10;
        int i11 = this.f23949e;
        boolean z10 = f23945g;
        int i12 = this.f23948d;
        byte[] bArr = this.f23947c;
        if (!z10 || i12 - i11 < 10) {
            while ((j10 & (-128)) != 0) {
                i10 = i11 + 1;
                try {
                    bArr[i11] = (byte) (((int) j10) | 128);
                    j10 >>>= 7;
                    i11 = i10;
                } catch (IndexOutOfBoundsException e10) {
                    throw new CodedOutputStream$OutOfSpaceException(i10, i12, 1, e10);
                }
            }
            i10 = i11 + 1;
            bArr[i11] = (byte) j10;
        } else {
            while ((j10 & (-128)) != 0) {
                l1.l(bArr, i11, (byte) (((int) j10) | 128));
                j10 >>>= 7;
                i11++;
            }
            i10 = i11 + 1;
            l1.l(bArr, i11, (byte) j10);
        }
        this.f23949e = i10;
    }

    public final void t0(byte b10) {
        int i10 = this.f23949e;
        try {
            int i11 = i10 + 1;
            try {
                this.f23947c[i10] = b10;
                this.f23949e = i11;
            } catch (IndexOutOfBoundsException e10) {
                e = e10;
                i10 = i11;
                throw new CodedOutputStream$OutOfSpaceException(i10, this.f23948d, 1, e);
            }
        } catch (IndexOutOfBoundsException e11) {
            e = e11;
        }
    }

    public final void u0(byte[] bArr, int i10, int i11) throws CodedOutputStream$OutOfSpaceException {
        try {
            System.arraycopy(bArr, i10, this.f23947c, this.f23949e, i11);
            this.f23949e += i11;
        } catch (IndexOutOfBoundsException e10) {
            throw new CodedOutputStream$OutOfSpaceException(this.f23949e, this.f23948d, i11, e10);
        }
    }

    public final void v0(ByteString byteString) {
        F0(byteString.size());
        ByteString.LiteralByteString literalByteString = (ByteString.LiteralByteString) byteString;
        u0(literalByteString.bytes, literalByteString.l(), literalByteString.size());
    }

    public final void w0(int i10, int i11) {
        D0(i10, 5);
        x0(i11);
    }

    public final void x0(int i10) {
        int i11 = this.f23949e;
        try {
            byte[] bArr = this.f23947c;
            bArr[i11] = (byte) i10;
            bArr[i11 + 1] = (byte) (i10 >> 8);
            bArr[i11 + 2] = (byte) (i10 >> 16);
            bArr[i11 + 3] = (byte) (i10 >> 24);
            this.f23949e = i11 + 4;
        } catch (IndexOutOfBoundsException e10) {
            throw new CodedOutputStream$OutOfSpaceException(i11, this.f23948d, 4, e10);
        }
    }

    public final void y0(int i10, long j10) {
        D0(i10, 1);
        z0(j10);
    }

    public final void z0(long j10) {
        int i10 = this.f23949e;
        try {
            byte[] bArr = this.f23947c;
            bArr[i10] = (byte) j10;
            bArr[i10 + 1] = (byte) (j10 >> 8);
            bArr[i10 + 2] = (byte) (j10 >> 16);
            bArr[i10 + 3] = (byte) (j10 >> 24);
            bArr[i10 + 4] = (byte) (j10 >> 32);
            bArr[i10 + 5] = (byte) (j10 >> 40);
            bArr[i10 + 6] = (byte) (j10 >> 48);
            bArr[i10 + 7] = (byte) (j10 >> 56);
            this.f23949e = i10 + 8;
        } catch (IndexOutOfBoundsException e10) {
            throw new CodedOutputStream$OutOfSpaceException(i10, this.f23948d, 8, e10);
        }
    }
}
