package x0;

import C3.J;
import d5.AbstractC0844a;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public final class o {

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

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

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

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

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

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

    public o() {
        this.f18539a = v.f18555c;
    }

    public final long A() {
        int i;
        int i5;
        long j8 = this.f18539a[this.f18540b];
        int i8 = 7;
        while (true) {
            if (i8 < 0) {
                break;
            }
            int i9 = 1 << i8;
            if ((((long) i9) & j8) != 0) {
                i8--;
            } else if (i8 < 6) {
                j8 &= (long) (i9 - 1);
                i5 = 7 - i8;
            } else if (i8 == 7) {
                i5 = 1;
            }
        }
        i5 = 0;
        if (i5 == 0) {
            throw new NumberFormatException(AbstractC0844a.i(j8, "Invalid UTF-8 sequence first byte: "));
        }
        for (i = 1; i < i5; i++) {
            byte b8 = this.f18539a[this.f18540b + i];
            if ((b8 & 192) != 128) {
                throw new NumberFormatException(AbstractC0844a.i(j8, "Invalid UTF-8 sequence continuation byte: "));
            }
            j8 = (j8 << 6) | ((long) (b8 & 63));
        }
        this.f18540b += i5;
        return j8;
    }

    public final Charset B() {
        if (a() >= 3) {
            byte[] bArr = this.f18539a;
            int i = this.f18540b;
            if (bArr[i] == -17 && bArr[i + 1] == -69 && bArr[i + 2] == -65) {
                this.f18540b = i + 3;
                return StandardCharsets.UTF_8;
            }
        }
        if (a() < 2) {
            return null;
        }
        byte[] bArr2 = this.f18539a;
        int i5 = this.f18540b;
        byte b8 = bArr2[i5];
        if (b8 == -2 && bArr2[i5 + 1] == -1) {
            this.f18540b = i5 + 2;
            return StandardCharsets.UTF_16BE;
        }
        if (b8 != -1 || bArr2[i5 + 1] != -2) {
            return null;
        }
        this.f18540b = i5 + 2;
        return StandardCharsets.UTF_16LE;
    }

    public final void C(int i) {
        byte[] bArr = this.f18539a;
        if (bArr.length < i) {
            bArr = new byte[i];
        }
        D(i, bArr);
    }

    public final void D(int i, byte[] bArr) {
        this.f18539a = bArr;
        this.f18541c = i;
        this.f18540b = 0;
    }

    public final void E(int i) {
        AbstractC1709a.d(i >= 0 && i <= this.f18539a.length);
        this.f18541c = i;
    }

    public final void F(int i) {
        AbstractC1709a.d(i >= 0 && i <= this.f18541c);
        this.f18540b = i;
    }

    public final void G(int i) {
        F(this.f18540b + i);
    }

    public final int a() {
        return this.f18541c - this.f18540b;
    }

    public final void b(int i) {
        byte[] bArr = this.f18539a;
        if (i > bArr.length) {
            this.f18539a = Arrays.copyOf(bArr, i);
        }
    }

    public final char c(Charset charset) {
        AbstractC1709a.c("Unsupported charset: " + charset, f18538f.contains(charset));
        return (char) (d(charset) >> 16);
    }

    public final int d(Charset charset) {
        byte b8;
        byte b9 = 0;
        int i = 1;
        if ((charset.equals(StandardCharsets.UTF_8) || charset.equals(StandardCharsets.US_ASCII)) && a() >= 1) {
            b8 = this.f18539a[this.f18540b];
        } else {
            if ((charset.equals(StandardCharsets.UTF_16) || charset.equals(StandardCharsets.UTF_16BE)) && a() >= 2) {
                byte[] bArr = this.f18539a;
                int i5 = this.f18540b;
                b9 = bArr[i5];
                b8 = bArr[i5 + 1];
            } else {
                if (!charset.equals(StandardCharsets.UTF_16LE) || a() < 2) {
                    return 0;
                }
                byte[] bArr2 = this.f18539a;
                int i8 = this.f18540b;
                b9 = bArr2[i8 + 1];
                b8 = bArr2[i8];
            }
            i = 2;
        }
        return ((b8 & 255) << 16) | (b9 << 24) | (i & 255);
    }

    public final void e(byte[] bArr, int i, int i5) {
        System.arraycopy(this.f18539a, this.f18540b, bArr, i, i5);
        this.f18540b += i5;
    }

    public final char f(Charset charset, char[] cArr) {
        int iD = d(charset);
        if (iD != 0) {
            char c3 = (char) (iD >> 16);
            for (char c8 : cArr) {
                if (c8 == c3) {
                    this.f18540b += iD & 65535;
                    return c3;
                }
            }
        }
        return (char) 0;
    }

    public final int g() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        int i8 = (bArr[i] & 255) << 24;
        int i9 = i + 2;
        this.f18540b = i9;
        int i10 = ((bArr[i5] & 255) << 16) | i8;
        int i11 = i + 3;
        this.f18540b = i11;
        int i12 = i10 | ((bArr[i9] & 255) << 8);
        this.f18540b = i + 4;
        return (bArr[i11] & 255) | i12;
    }

    /* JADX WARN: Removed duplicated region for block: B:44:0x00ad  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String h(Charset charset) {
        int i;
        AbstractC1709a.c("Unsupported charset: " + charset, f18538f.contains(charset));
        if (a() == 0) {
            return null;
        }
        Charset charset2 = StandardCharsets.US_ASCII;
        if (!charset.equals(charset2)) {
            B();
        }
        if (charset.equals(StandardCharsets.UTF_8) || charset.equals(charset2)) {
            i = 1;
        } else {
            if (!charset.equals(StandardCharsets.UTF_16) && !charset.equals(StandardCharsets.UTF_16LE) && !charset.equals(StandardCharsets.UTF_16BE)) {
                throw new IllegalArgumentException("Unsupported charset: " + charset);
            }
            i = 2;
        }
        int i5 = this.f18540b;
        while (true) {
            int i8 = this.f18541c;
            if (i5 >= i8 - (i - 1)) {
                i5 = i8;
                break;
            }
            if ((charset.equals(StandardCharsets.UTF_8) || charset.equals(StandardCharsets.US_ASCII)) && v.J(this.f18539a[i5])) {
                break;
            }
            if (charset.equals(StandardCharsets.UTF_16) || charset.equals(StandardCharsets.UTF_16BE)) {
                byte[] bArr = this.f18539a;
                if (bArr[i5] == 0 && v.J(bArr[i5 + 1])) {
                    break;
                }
                if (charset.equals(StandardCharsets.UTF_16LE)) {
                    byte[] bArr2 = this.f18539a;
                    if (bArr2[i5 + 1] == 0 && v.J(bArr2[i5])) {
                        break;
                    }
                }
                i5 += i;
            }
        }
        String strR = r(i5 - this.f18540b, charset);
        if (this.f18540b != this.f18541c && f(charset, f18536d) == '\r') {
            f(charset, f18537e);
        }
        return strR;
    }

    public final int i() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        int i8 = bArr[i] & 255;
        int i9 = i + 2;
        this.f18540b = i9;
        int i10 = ((bArr[i5] & 255) << 8) | i8;
        int i11 = i + 3;
        this.f18540b = i11;
        int i12 = i10 | ((bArr[i9] & 255) << 16);
        this.f18540b = i + 4;
        return ((bArr[i11] & 255) << 24) | i12;
    }

    public final long j() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        long j8 = ((long) bArr[i]) & 255;
        int i8 = i + 2;
        this.f18540b = i8;
        long j9 = j8 | ((((long) bArr[i5]) & 255) << 8);
        int i9 = i + 3;
        this.f18540b = i9;
        long j10 = j9 | ((((long) bArr[i8]) & 255) << 16);
        int i10 = i + 4;
        this.f18540b = i10;
        long j11 = j10 | ((((long) bArr[i9]) & 255) << 24);
        int i11 = i + 5;
        this.f18540b = i11;
        long j12 = j11 | ((((long) bArr[i10]) & 255) << 32);
        int i12 = i + 6;
        this.f18540b = i12;
        long j13 = j12 | ((((long) bArr[i11]) & 255) << 40);
        int i13 = i + 7;
        this.f18540b = i13;
        long j14 = j13 | ((((long) bArr[i12]) & 255) << 48);
        this.f18540b = i + 8;
        return ((((long) bArr[i13]) & 255) << 56) | j14;
    }

    public final long k() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        long j8 = ((long) bArr[i]) & 255;
        int i8 = i + 2;
        this.f18540b = i8;
        long j9 = j8 | ((((long) bArr[i5]) & 255) << 8);
        int i9 = i + 3;
        this.f18540b = i9;
        long j10 = j9 | ((((long) bArr[i8]) & 255) << 16);
        this.f18540b = i + 4;
        return ((((long) bArr[i9]) & 255) << 24) | j10;
    }

    public final int l() {
        int i = i();
        if (i >= 0) {
            return i;
        }
        throw new IllegalStateException(AbstractC0844a.j("Top bit not zero: ", i));
    }

    public final int m() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        int i8 = bArr[i] & 255;
        this.f18540b = i + 2;
        return ((bArr[i5] & 255) << 8) | i8;
    }

    public final long n() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        long j8 = (((long) bArr[i]) & 255) << 56;
        int i8 = i + 2;
        this.f18540b = i8;
        long j9 = j8 | ((((long) bArr[i5]) & 255) << 48);
        int i9 = i + 3;
        this.f18540b = i9;
        long j10 = j9 | ((((long) bArr[i8]) & 255) << 40);
        int i10 = i + 4;
        this.f18540b = i10;
        long j11 = j10 | ((((long) bArr[i9]) & 255) << 32);
        int i11 = i + 5;
        this.f18540b = i11;
        long j12 = j11 | ((((long) bArr[i10]) & 255) << 24);
        int i12 = i + 6;
        this.f18540b = i12;
        long j13 = j12 | ((((long) bArr[i11]) & 255) << 16);
        int i13 = i + 7;
        this.f18540b = i13;
        long j14 = j13 | ((((long) bArr[i12]) & 255) << 8);
        this.f18540b = i + 8;
        return (((long) bArr[i13]) & 255) | j14;
    }

    public final String o() {
        if (a() == 0) {
            return null;
        }
        int i = this.f18540b;
        while (i < this.f18541c && this.f18539a[i] != 0) {
            i++;
        }
        byte[] bArr = this.f18539a;
        int i5 = this.f18540b;
        int i8 = v.f18553a;
        String str = new String(bArr, i5, i - i5, StandardCharsets.UTF_8);
        this.f18540b = i;
        if (i < this.f18541c) {
            this.f18540b = i + 1;
        }
        return str;
    }

    public final String p(int i) {
        if (i == 0) {
            return "";
        }
        int i5 = this.f18540b;
        int i8 = (i5 + i) - 1;
        int i9 = (i8 >= this.f18541c || this.f18539a[i8] != 0) ? i : i - 1;
        byte[] bArr = this.f18539a;
        int i10 = v.f18553a;
        String str = new String(bArr, i5, i9, StandardCharsets.UTF_8);
        this.f18540b += i;
        return str;
    }

    public final short q() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        int i8 = (bArr[i] & 255) << 8;
        this.f18540b = i + 2;
        return (short) ((bArr[i5] & 255) | i8);
    }

    public final String r(int i, Charset charset) {
        String str = new String(this.f18539a, this.f18540b, i, charset);
        this.f18540b += i;
        return str;
    }

    public final int s() {
        return (t() << 21) | (t() << 14) | (t() << 7) | t();
    }

    public final int t() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        this.f18540b = i + 1;
        return bArr[i] & 255;
    }

    public final int u() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        int i8 = (bArr[i] & 255) << 8;
        this.f18540b = i + 2;
        int i9 = (bArr[i5] & 255) | i8;
        this.f18540b = i + 4;
        return i9;
    }

    public final long v() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        long j8 = (((long) bArr[i]) & 255) << 24;
        int i8 = i + 2;
        this.f18540b = i8;
        long j9 = j8 | ((((long) bArr[i5]) & 255) << 16);
        int i9 = i + 3;
        this.f18540b = i9;
        long j10 = j9 | ((((long) bArr[i8]) & 255) << 8);
        this.f18540b = i + 4;
        return (((long) bArr[i9]) & 255) | j10;
    }

    public final int w() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        int i8 = (bArr[i] & 255) << 16;
        int i9 = i + 2;
        this.f18540b = i9;
        int i10 = ((bArr[i5] & 255) << 8) | i8;
        this.f18540b = i + 3;
        return (bArr[i9] & 255) | i10;
    }

    public final int x() {
        int iG = g();
        if (iG >= 0) {
            return iG;
        }
        throw new IllegalStateException(AbstractC0844a.j("Top bit not zero: ", iG));
    }

    public final long y() {
        long jN = n();
        if (jN >= 0) {
            return jN;
        }
        throw new IllegalStateException(AbstractC0844a.i(jN, "Top bit not zero: "));
    }

    public final int z() {
        byte[] bArr = this.f18539a;
        int i = this.f18540b;
        int i5 = i + 1;
        this.f18540b = i5;
        int i8 = (bArr[i] & 255) << 8;
        this.f18540b = i + 2;
        return (bArr[i5] & 255) | i8;
    }

    public o(int i) {
        this.f18539a = new byte[i];
        this.f18541c = i;
    }

    public o(byte[] bArr) {
        this.f18539a = bArr;
        this.f18541c = bArr.length;
    }

    public o(int i, byte[] bArr) {
        this.f18539a = bArr;
        this.f18541c = i;
    }
}
