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

import java.util.logging.Logger;

/* JADX INFO: renamed from: com.google.crypto.tink.shaded.protobuf.k, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0745k extends W2.g {

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final boolean f10515m = n0.f10528e;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public K f10516h;
    public final byte[] i;

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

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

    public C0745k(int i, byte[] bArr) {
        if (((bArr.length - i) | i) < 0) {
            throw new IllegalArgumentException(String.format("Array range is invalid. Buffer.length=%d, offset=%d, length=%d", Integer.valueOf(bArr.length), 0, Integer.valueOf(i)));
        }
        this.i = bArr;
        this.f10518k = 0;
        this.f10517j = i;
    }

    public static int O(int i, AbstractC0742h abstractC0742h) {
        return P(abstractC0742h) + V(i);
    }

    public static int P(AbstractC0742h abstractC0742h) {
        int size = abstractC0742h.size();
        return W(size) + size;
    }

    public static int Q(int i) {
        return V(i) + 4;
    }

    public static int R(int i) {
        return V(i) + 8;
    }

    public static int S(int i, AbstractC0735a abstractC0735a, b0 b0Var) {
        return abstractC0735a.a(b0Var) + (V(i) * 2);
    }

    public static int T(int i) {
        if (i >= 0) {
            return W(i);
        }
        return 10;
    }

    public static int U(String str) {
        int length;
        try {
            length = q0.b(str);
        } catch (p0 unused) {
            length = str.getBytes(AbstractC0759z.f10544a).length;
        }
        return W(length) + length;
    }

    public static int V(int i) {
        return W(i << 3);
    }

    public static int W(int i) {
        if ((i & (-128)) == 0) {
            return 1;
        }
        if ((i & (-16384)) == 0) {
            return 2;
        }
        if (((-2097152) & i) == 0) {
            return 3;
        }
        return (i & (-268435456)) == 0 ? 4 : 5;
    }

    public static int X(long j8) {
        int i;
        if (((-128) & j8) == 0) {
            return 1;
        }
        if (j8 < 0) {
            return 10;
        }
        if (((-34359738368L) & j8) != 0) {
            j8 >>>= 28;
            i = 6;
        } else {
            i = 2;
        }
        if (((-2097152) & j8) != 0) {
            i += 2;
            j8 >>>= 14;
        }
        return (j8 & (-16384)) != 0 ? i + 1 : i;
    }

    public final void Y(byte b8) throws F0.t {
        try {
            byte[] bArr = this.i;
            int i = this.f10518k;
            this.f10518k = i + 1;
            bArr[i] = b8;
        } catch (IndexOutOfBoundsException e8) {
            throw new F0.t(String.format("Pos: %d, limit: %d, len: %d", Integer.valueOf(this.f10518k), Integer.valueOf(this.f10517j), 1), e8);
        }
    }

    public final void Z(byte[] bArr, int i, int i5) throws F0.t {
        try {
            System.arraycopy(bArr, i, this.i, this.f10518k, i5);
            this.f10518k += i5;
        } catch (IndexOutOfBoundsException e8) {
            throw new F0.t(String.format("Pos: %d, limit: %d, len: %d", Integer.valueOf(this.f10518k), Integer.valueOf(this.f10517j), Integer.valueOf(i5)), e8);
        }
    }

    public final void a0(int i, int i5) throws F0.t {
        f0(i, 5);
        b0(i5);
    }

    public final void b0(int i) throws F0.t {
        try {
            byte[] bArr = this.i;
            int i5 = this.f10518k;
            int i8 = i5 + 1;
            this.f10518k = i8;
            bArr[i5] = (byte) (i & 255);
            int i9 = i5 + 2;
            this.f10518k = i9;
            bArr[i8] = (byte) ((i >> 8) & 255);
            int i10 = i5 + 3;
            this.f10518k = i10;
            bArr[i9] = (byte) ((i >> 16) & 255);
            this.f10518k = i5 + 4;
            bArr[i10] = (byte) ((i >> 24) & 255);
        } catch (IndexOutOfBoundsException e8) {
            throw new F0.t(String.format("Pos: %d, limit: %d, len: %d", Integer.valueOf(this.f10518k), Integer.valueOf(this.f10517j), 1), e8);
        }
    }

    public final void c0(long j8, int i) throws F0.t {
        f0(i, 1);
        d0(j8);
    }

    public final void d0(long j8) throws F0.t {
        try {
            byte[] bArr = this.i;
            int i = this.f10518k;
            int i5 = i + 1;
            this.f10518k = i5;
            bArr[i] = (byte) (((int) j8) & 255);
            int i8 = i + 2;
            this.f10518k = i8;
            bArr[i5] = (byte) (((int) (j8 >> 8)) & 255);
            int i9 = i + 3;
            this.f10518k = i9;
            bArr[i8] = (byte) (((int) (j8 >> 16)) & 255);
            int i10 = i + 4;
            this.f10518k = i10;
            bArr[i9] = (byte) (((int) (j8 >> 24)) & 255);
            int i11 = i + 5;
            this.f10518k = i11;
            bArr[i10] = (byte) (((int) (j8 >> 32)) & 255);
            int i12 = i + 6;
            this.f10518k = i12;
            bArr[i11] = (byte) (((int) (j8 >> 40)) & 255);
            int i13 = i + 7;
            this.f10518k = i13;
            bArr[i12] = (byte) (((int) (j8 >> 48)) & 255);
            this.f10518k = i + 8;
            bArr[i13] = (byte) (((int) (j8 >> 56)) & 255);
        } catch (IndexOutOfBoundsException e8) {
            throw new F0.t(String.format("Pos: %d, limit: %d, len: %d", Integer.valueOf(this.f10518k), Integer.valueOf(this.f10517j), 1), e8);
        }
    }

    public final void e0(int i) throws F0.t {
        if (i >= 0) {
            g0(i);
        } else {
            i0(i);
        }
    }

    public final void f0(int i, int i5) throws F0.t {
        g0((i << 3) | i5);
    }

    public final void g0(int i) throws F0.t {
        while (true) {
            int i5 = i & (-128);
            byte[] bArr = this.i;
            if (i5 == 0) {
                int i8 = this.f10518k;
                this.f10518k = i8 + 1;
                bArr[i8] = (byte) i;
                return;
            } else {
                try {
                    int i9 = this.f10518k;
                    this.f10518k = i9 + 1;
                    bArr[i9] = (byte) ((i & 127) | 128);
                    i >>>= 7;
                } catch (IndexOutOfBoundsException e8) {
                    throw new F0.t(String.format("Pos: %d, limit: %d, len: %d", Integer.valueOf(this.f10518k), Integer.valueOf(this.f10517j), 1), e8);
                }
            }
            throw new F0.t(String.format("Pos: %d, limit: %d, len: %d", Integer.valueOf(this.f10518k), Integer.valueOf(this.f10517j), 1), e8);
        }
    }

    public final void h0(long j8, int i) throws F0.t {
        f0(i, 0);
        i0(j8);
    }

    public final void i0(long j8) throws F0.t {
        byte[] bArr = this.i;
        boolean z8 = f10515m;
        int i = this.f10517j;
        if (z8 && i - this.f10518k >= 10) {
            while ((j8 & (-128)) != 0) {
                int i5 = this.f10518k;
                this.f10518k = i5 + 1;
                n0.k(bArr, i5, (byte) ((((int) j8) & 127) | 128));
                j8 >>>= 7;
            }
            int i8 = this.f10518k;
            this.f10518k = 1 + i8;
            n0.k(bArr, i8, (byte) j8);
            return;
        }
        while ((j8 & (-128)) != 0) {
            try {
                int i9 = this.f10518k;
                this.f10518k = i9 + 1;
                bArr[i9] = (byte) ((((int) j8) & 127) | 128);
                j8 >>>= 7;
            } catch (IndexOutOfBoundsException e8) {
                throw new F0.t(String.format("Pos: %d, limit: %d, len: %d", Integer.valueOf(this.f10518k), Integer.valueOf(i), 1), e8);
            }
        }
        int i10 = this.f10518k;
        this.f10518k = i10 + 1;
        bArr[i10] = (byte) j8;
    }
}
