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

import androidx.annotation.Nullable;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import okio.Utf8;

/* JADX INFO: loaded from: classes2.dex */
public final class p01 {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final char[] f15221d = {'\r', '\n'};

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final char[] f15222e = {'\n'};

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final zzgup f15223f = zzgup.t(5, StandardCharsets.US_ASCII, StandardCharsets.UTF_8, StandardCharsets.UTF_16, StandardCharsets.UTF_16BE, StandardCharsets.UTF_16LE);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final AtomicBoolean f15224g = new AtomicBoolean();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public byte[] f15225a;

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

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

    public p01(byte[] bArr, int i10) {
        this.f15225a = bArr;
        this.f15227c = i10;
    }

    public static int r(Charset charset) {
        ih1.R0(f15223f.contains(charset), "Unsupported charset: %s", charset);
        return (charset.equals(StandardCharsets.UTF_8) || charset.equals(StandardCharsets.US_ASCII)) ? 1 : 2;
    }

    public static boolean t(byte b10) {
        return (b10 & 192) == 128;
    }

    public static int u(int i10, int i11, int i12, int i13) {
        return za2.G((byte) 0, ih1.c(((i10 & 7) << 2) | ((i11 & 48) >> 4)), ih1.c(((i12 & 60) >> 2) | ((i11 & 15) << 4)), ih1.c((i13 & 63) | ((i12 & 3) << 6)));
    }

    public final void A(int i10) {
        boolean z10 = false;
        if (i10 >= 0 && i10 <= this.f15225a.length) {
            z10 = true;
        }
        ih1.x(z10);
        this.f15227c = i10;
    }

    public final int B() {
        return this.f15226b;
    }

    public final void C(int i10) {
        boolean z10 = false;
        if (i10 >= 0 && i10 <= this.f15227c) {
            z10 = true;
        }
        ih1.x(z10);
        this.f15226b = i10;
    }

    public final byte[] D() {
        return this.f15225a;
    }

    public final void E(int i10) {
        C(this.f15226b + i10);
    }

    public final void F(int i10, int i11, byte[] bArr) {
        s(i11);
        System.arraycopy(this.f15225a, this.f15226b, bArr, i10, i11);
        this.f15226b += i11;
    }

    public final int G() {
        s(1);
        return this.f15225a[this.f15226b] & 255;
    }

    public final int H() {
        if (y() >= 4) {
            this.f15226b -= 4;
            return b();
        }
        int i10 = this.f15226b;
        int i11 = this.f15227c;
        throw new IndexOutOfBoundsException(com.google.android.gms.measurement.internal.u1.m(new StringBuilder(String.valueOf(i10).length() + 17 + String.valueOf(i11).length()), "position=", i10, ", limit=", i11));
    }

    public final int I() {
        s(1);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        this.f15226b = i10 + 1;
        return bArr[i10] & 255;
    }

    public final int J() {
        s(2);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        int i11 = i10 + 1;
        int i12 = bArr[i10] & 255;
        this.f15226b = i10 + 2;
        return (bArr[i11] & 255) | (i12 << 8);
    }

    public final int K() {
        s(2);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        int i11 = i10 + 1;
        int i12 = bArr[i10] & 255;
        this.f15226b = i10 + 2;
        return ((bArr[i11] & 255) << 8) | i12;
    }

    public final short L() {
        s(2);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        int i11 = i10 + 1;
        int i12 = bArr[i10] & 255;
        this.f15226b = i10 + 2;
        return (short) ((bArr[i11] & 255) | (i12 << 8));
    }

    public final int M() {
        s(3);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        int i11 = bArr[i10] & 255;
        int i12 = i10 + 2;
        int i13 = bArr[i10 + 1] & 255;
        this.f15226b = i10 + 3;
        return (bArr[i12] & 255) | (i11 << 16) | (i13 << 8);
    }

    public final long N() {
        s(4);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        long j10 = bArr[i10];
        long j11 = bArr[i10 + 1];
        long j12 = bArr[i10 + 2];
        this.f15226b = i10 + 4;
        return (((long) bArr[i10 + 3]) & 255) | ((j10 & 255) << 24) | ((j11 & 255) << 16) | ((j12 & 255) << 8);
    }

    public final long a() {
        s(4);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        long j10 = bArr[i10];
        long j11 = bArr[i10 + 1];
        long j12 = bArr[i10 + 2];
        this.f15226b = i10 + 4;
        return ((((long) bArr[i10 + 3]) & 255) << 24) | (255 & j10) | ((j11 & 255) << 8) | ((j12 & 255) << 16);
    }

    public final int b() {
        s(4);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        int i11 = bArr[i10] & 255;
        int i12 = bArr[i10 + 1] & 255;
        int i13 = i10 + 3;
        int i14 = bArr[i10 + 2] & 255;
        this.f15226b = i10 + 4;
        return (bArr[i13] & 255) | (i11 << 24) | (i12 << 16) | (i14 << 8);
    }

    public final int c() {
        s(4);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        int i11 = bArr[i10] & 255;
        int i12 = bArr[i10 + 1] & 255;
        int i13 = bArr[i10 + 2] & 255;
        this.f15226b = i10 + 4;
        return ((bArr[i10 + 3] & 255) << 24) | (i12 << 8) | i11 | (i13 << 16);
    }

    public final long d() {
        s(8);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        long j10 = bArr[i10];
        long j11 = bArr[i10 + 1];
        long j12 = bArr[i10 + 2];
        long j13 = bArr[i10 + 3];
        long j14 = bArr[i10 + 4];
        long j15 = bArr[i10 + 5];
        int i11 = i10 + 7;
        long j16 = bArr[i10 + 6];
        this.f15226b = i10 + 8;
        return ((j16 & 255) << 8) | ((j10 & 255) << 56) | ((j11 & 255) << 48) | ((j12 & 255) << 40) | ((j13 & 255) << 32) | ((j14 & 255) << 24) | ((j15 & 255) << 16) | (((long) bArr[i11]) & 255);
    }

    public final long e() {
        s(8);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        long j10 = bArr[i10];
        long j11 = bArr[i10 + 1];
        long j12 = bArr[i10 + 2];
        long j13 = bArr[i10 + 3];
        long j14 = bArr[i10 + 4];
        long j15 = bArr[i10 + 5];
        int i11 = i10 + 7;
        long j16 = bArr[i10 + 6];
        this.f15226b = i10 + 8;
        return ((j15 & 255) << 40) | (j10 & 255) | ((j11 & 255) << 8) | ((j12 & 255) << 16) | ((j13 & 255) << 24) | ((j14 & 255) << 32) | ((j16 & 255) << 48) | ((((long) bArr[i11]) & 255) << 56);
    }

    public final int f() {
        s(4);
        byte[] bArr = this.f15225a;
        int i10 = this.f15226b;
        int i11 = bArr[i10] & 255;
        int i12 = bArr[i10 + 1] & 255;
        this.f15226b = i10 + 4;
        return (i11 << 8) | i12;
    }

    public final int g() {
        return (I() << 21) | (I() << 14) | (I() << 7) | I();
    }

    public final int h() {
        int iB = b();
        if (iB >= 0) {
            return iB;
        }
        throw new IllegalStateException(defpackage.e.n(new StringBuilder(String.valueOf(iB).length() + 18), "Top bit not zero: ", iB));
    }

    public final int i() {
        int iC = c();
        if (iC >= 0) {
            return iC;
        }
        throw new IllegalStateException(defpackage.e.n(new StringBuilder(String.valueOf(iC).length() + 18), "Top bit not zero: ", iC));
    }

    public final long j() {
        long jD = d();
        if (jD >= 0) {
            return jD;
        }
        StringBuilder sb = new StringBuilder(String.valueOf(jD).length() + 18);
        sb.append("Top bit not zero: ");
        sb.append(jD);
        throw new IllegalStateException(sb.toString());
    }

    public final String k(int i10, Charset charset) {
        s(i10);
        byte[] bArr = this.f15225a;
        int i11 = this.f15226b;
        String str = new String(bArr, i11, i10, charset);
        this.f15226b = i11 + i10;
        return str;
    }

    public final String l(int i10) {
        s(i10);
        if (i10 == 0) {
            return "";
        }
        int i11 = this.f15226b;
        int i12 = (i11 + i10) - 1;
        int i13 = (i12 >= this.f15227c || this.f15225a[i12] != 0) ? i10 : i10 - 1;
        byte[] bArr = this.f15225a;
        String str = bb1.f9474a;
        String str2 = new String(bArr, i11, i13, StandardCharsets.UTF_8);
        this.f15226b += i10;
        return str2;
    }

    public final long m() {
        int i10;
        s(1);
        long j10 = this.f15225a[this.f15226b];
        int i11 = 7;
        while (true) {
            i10 = 0;
            if (i11 < 0) {
                break;
            }
            int i12 = 1 << i11;
            if ((((long) i12) & j10) != 0) {
                i11--;
            } else if (i11 < 6) {
                j10 &= (long) (i12 - 1);
                i10 = 7 - i11;
            } else if (i11 == 7) {
                i10 = 1;
            }
        }
        if (i10 == 0) {
            StringBuilder sb = new StringBuilder(String.valueOf(j10).length() + 35);
            sb.append("Invalid UTF-8 sequence first byte: ");
            sb.append(j10);
            throw new NumberFormatException(sb.toString());
        }
        s(i10);
        for (int i13 = 1; i13 < i10; i13++) {
            byte b10 = this.f15225a[this.f15226b + i13];
            if ((b10 & 192) != 128) {
                StringBuilder sb2 = new StringBuilder(String.valueOf(j10).length() + 42);
                sb2.append("Invalid UTF-8 sequence continuation byte: ");
                sb2.append(j10);
                throw new NumberFormatException(sb2.toString());
            }
            j10 = (j10 << 6) | ((long) (b10 & Utf8.REPLACEMENT_BYTE));
        }
        this.f15226b += i10;
        return j10;
    }

    public final long n() {
        long j10 = 0;
        for (int i10 = 0; i10 < 9; i10++) {
            if (this.f15226b == this.f15227c) {
                throw new IllegalStateException("Attempting to read a byte over the limit.");
            }
            long jI = I();
            j10 |= (127 & jI) << (i10 * 7);
            if ((jI & 128) == 0) {
                break;
            }
        }
        return j10;
    }

    public final char o(int i10, ByteOrder byteOrder) {
        byte b10;
        byte b11;
        s(2);
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
            byte[] bArr = this.f15225a;
            int i11 = this.f15226b + i10;
            b10 = bArr[i11];
            b11 = bArr[i11 + 1];
        } else {
            byte[] bArr2 = this.f15225a;
            int i12 = this.f15226b + i10;
            b10 = bArr2[i12 + 1];
            b11 = bArr2[i12];
        }
        return (char) ((b10 << 8) | (b11 & 255));
    }

    public final char p(Charset charset, char[] cArr) {
        int iQ;
        if (y() >= r(charset) && (iQ = q(charset)) != 0) {
            if (!Character.isSupplementaryCodePoint(i)) {
                long j10 = i;
                char c6 = (char) j10;
                ih1.L0("Out of range: %s", j10, ((long) c6) == j10);
                for (char c10 : cArr) {
                    if (c10 == c6) {
                        this.f15226b = za2.b(iQ & 255) + this.f15226b;
                        return c6;
                    }
                }
            }
        }
        return (char) 0;
    }

    public final int q(Charset charset) {
        int codePoint;
        int i10;
        int iU;
        ih1.R0(f15223f.contains(charset), "Unsupported charset: %s", charset);
        if (y() < r(charset)) {
            int i11 = this.f15226b;
            int i12 = this.f15227c;
            throw new IndexOutOfBoundsException(com.google.android.gms.measurement.internal.u1.m(new StringBuilder(String.valueOf(i11).length() + 17 + String.valueOf(i12).length()), "position=", i11, ", limit=", i12));
        }
        int i13 = 1;
        if (charset.equals(StandardCharsets.US_ASCII)) {
            byte b10 = this.f15225a[this.f15226b];
            if ((b10 & 128) == 0) {
                codePoint = b10 & 255;
                return (codePoint << 8) | i13;
            }
            return 0;
        }
        if (charset.equals(StandardCharsets.UTF_8)) {
            byte b11 = this.f15225a[this.f15226b];
            if ((b11 & 128) == 0) {
                i10 = 1;
            } else if ((b11 & 224) == 192 && y() >= 2 && t(this.f15225a[this.f15226b + 1])) {
                i10 = 2;
            } else {
                if ((this.f15225a[this.f15226b] & 240) == 224 && y() >= 3) {
                    byte[] bArr = this.f15225a;
                    int i14 = this.f15226b;
                    if (t(bArr[i14 + 1]) && t(bArr[i14 + 2])) {
                        i10 = 3;
                    }
                }
                if ((this.f15225a[this.f15226b] & 248) == 240 && y() >= 4) {
                    byte[] bArr2 = this.f15225a;
                    int i15 = this.f15226b;
                    if (t(bArr2[i15 + 1]) && t(bArr2[i15 + 2]) && t(bArr2[i15 + 3])) {
                        i10 = 4;
                    }
                }
                i10 = 0;
            }
            if (i10 == 1) {
                iU = this.f15225a[this.f15226b] & 255;
            } else if (i10 == 2) {
                byte[] bArr3 = this.f15225a;
                int i16 = this.f15226b;
                iU = u(0, 0, bArr3[i16], bArr3[i16 + 1]);
            } else {
                if (i10 != 3) {
                    if (i10 == 4) {
                        byte[] bArr4 = this.f15225a;
                        int i17 = this.f15226b;
                        iU = u(bArr4[i17], bArr4[i17 + 1], bArr4[i17 + 2], bArr4[i17 + 3]);
                    }
                    return 0;
                }
                byte[] bArr5 = this.f15225a;
                int i18 = this.f15226b;
                iU = u(0, bArr5[i18] & 15, bArr5[i18 + 1], bArr5[i18 + 2]);
            }
            i13 = i10;
            codePoint = iU;
        } else {
            ByteOrder byteOrder = charset.equals(StandardCharsets.UTF_16LE) ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;
            char cO = o(0, byteOrder);
            if (!Character.isHighSurrogate(cO) || y() < 4) {
                codePoint = cO;
                i13 = 2;
            } else {
                codePoint = Character.toCodePoint(cO, o(2, byteOrder));
                i13 = 4;
            }
        }
        return (codePoint << 8) | i13;
    }

    public final void s(int i10) {
        if (!f15224g.get() || y() >= i10) {
            return;
        }
        int iY = y();
        throw new IndexOutOfBoundsException(com.google.android.gms.measurement.internal.u1.m(new StringBuilder(String.valueOf(i10).length() + 25 + String.valueOf(iY).length()), "bytesNeeded= ", i10, ", bytesLeft=", iY));
    }

    public final void v(int i10) {
        byte[] bArr = this.f15225a;
        if (bArr.length < i10) {
            bArr = new byte[i10];
        }
        w(i10, bArr);
    }

    public final void w(int i10, byte[] bArr) {
        this.f15225a = bArr;
        this.f15227c = i10;
        this.f15226b = 0;
    }

    public final void x(int i10) {
        byte[] bArr = this.f15225a;
        if (i10 > bArr.length) {
            this.f15225a = Arrays.copyOf(bArr, i10);
        }
    }

    public final int y() {
        return Math.max(this.f15227c - this.f15226b, 0);
    }

    public final int z() {
        return this.f15227c;
    }

    @Nullable
    public final String zzM(char c6) {
        if (y() == 0) {
            return null;
        }
        int i10 = this.f15226b;
        while (i10 < this.f15227c && this.f15225a[i10] != 0) {
            i10++;
        }
        byte[] bArr = this.f15225a;
        int i11 = this.f15226b;
        String str = bb1.f9474a;
        String str2 = new String(bArr, i11, i10 - i11, StandardCharsets.UTF_8);
        this.f15226b = i10;
        if (i10 < this.f15227c) {
            this.f15226b = i10 + 1;
        }
        return str2;
    }

    /* JADX WARN: Removed duplicated region for block: B:34:0x0081  */
    /* JADX WARN: Removed duplicated region for block: B:44:0x00a2  */
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String zzN(Charset charset) {
        ih1.R0(f15223f.contains(charset), "Unsupported charset: %s", charset);
        if (y() == 0) {
            return null;
        }
        Charset charset2 = StandardCharsets.US_ASCII;
        if (!charset.equals(charset2)) {
            zzR();
        }
        int i10 = 1;
        if (!charset.equals(StandardCharsets.UTF_8) && !charset.equals(charset2)) {
            i10 = 2;
            if (!charset.equals(StandardCharsets.UTF_16) && !charset.equals(StandardCharsets.UTF_16LE) && !charset.equals(StandardCharsets.UTF_16BE)) {
                throw new IllegalArgumentException("Unsupported charset: ".concat(String.valueOf(charset)));
            }
        }
        int i11 = this.f15226b;
        while (true) {
            int i12 = this.f15227c;
            if (i11 >= i12 - (i10 - 1)) {
                i11 = i12;
                break;
            }
            if (charset.equals(StandardCharsets.UTF_8) || charset.equals(StandardCharsets.US_ASCII)) {
                byte b10 = this.f15225a[i11];
                String str = bb1.f9474a;
                if (b10 == 10 || b10 == 13) {
                    break;
                }
                if (charset.equals(StandardCharsets.UTF_16) || charset.equals(StandardCharsets.UTF_16BE)) {
                    byte[] bArr = this.f15225a;
                    if (bArr[i11] == 0) {
                        byte b11 = bArr[i11 + 1];
                        String str2 = bb1.f9474a;
                        if (b11 == 10 || b11 == 13) {
                            break;
                        }
                        if (charset.equals(StandardCharsets.UTF_16LE)) {
                            byte[] bArr2 = this.f15225a;
                            if (bArr2[i11 + 1] == 0) {
                                byte b12 = bArr2[i11];
                                String str3 = bb1.f9474a;
                                if (b12 == 10 || b12 == 13) {
                                    break;
                                }
                            } else {
                                continue;
                            }
                        }
                        i11 += i10;
                    }
                }
            }
        }
        String strK = k(i11 - this.f15226b, charset);
        if (this.f15226b != this.f15227c && p(charset, f15221d) == '\r') {
            p(charset, f15222e);
        }
        return strK;
    }

    @Nullable
    public final Charset zzR() {
        if (y() >= 3) {
            byte[] bArr = this.f15225a;
            int i10 = this.f15226b;
            if (bArr[i10] == -17 && bArr[i10 + 1] == -69 && bArr[i10 + 2] == -65) {
                this.f15226b = i10 + 3;
                return StandardCharsets.UTF_8;
            }
        }
        if (y() < 2) {
            return null;
        }
        byte[] bArr2 = this.f15225a;
        int i11 = this.f15226b;
        byte b10 = bArr2[i11];
        if (b10 == -2) {
            if (bArr2[i11 + 1] != -1) {
                return null;
            }
            this.f15226b = i11 + 2;
            return StandardCharsets.UTF_16BE;
        }
        if (b10 != -1 || bArr2[i11 + 1] != -2) {
            return null;
        }
        this.f15226b = i11 + 2;
        return StandardCharsets.UTF_16LE;
    }

    public p01() {
        this.f15225a = bb1.f9475b;
    }

    public p01(int i10) {
        this.f15225a = new byte[i10];
        this.f15227c = i10;
    }

    public p01(byte[] bArr) {
        this.f15225a = bArr;
        this.f15227c = bArr.length;
    }
}
