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

import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.AccessController;
import java.util.logging.Level;
import java.util.logging.Logger;
import libcore.io.Memory;
import sun.misc.Unsafe;

/* JADX INFO: loaded from: classes2.dex */
public abstract class sc2 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Unsafe f16405a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Class f16406b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final rc2 f16407c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final boolean f16408d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final boolean f16409e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final long f16410f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final long f16411g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final boolean f16412h;

    /* JADX WARN: Removed duplicated region for block: B:11:0x004b  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x007f  */
    /* JADX WARN: Removed duplicated region for block: B:4:0x001c  */
    static {
        rc2 qc2Var;
        Field declaredField;
        boolean z10;
        boolean z11;
        Field declaredField2;
        rc2 rc2Var;
        Unsafe unsafeT = t();
        f16405a = unsafeT;
        int i10 = fa2.f11138a;
        f16406b = Memory.class;
        Class<?> cls = Long.TYPE;
        boolean zU = u(cls);
        boolean zU2 = u(Integer.TYPE);
        if (unsafeT != null) {
            qc2Var = zU ? new qc2(unsafeT) : zU2 ? new pc2(unsafeT) : null;
        }
        f16407c = qc2Var;
        if (qc2Var == null) {
            z10 = false;
        } else {
            try {
                Class<?> cls2 = ((Unsafe) qc2Var.f16102a).getClass();
                cls2.getMethod("objectFieldOffset", Field.class);
                cls2.getMethod("getLong", Object.class, cls);
                try {
                    declaredField = Buffer.class.getDeclaredField("effectiveDirectAddress");
                } catch (Throwable unused) {
                    declaredField = null;
                }
                if (declaredField == null) {
                    try {
                        declaredField = Buffer.class.getDeclaredField("address");
                    } catch (Throwable unused2) {
                        declaredField = null;
                    }
                    if (declaredField != null) {
                        if (declaredField.getType() != Long.TYPE) {
                            declaredField = null;
                        }
                    }
                }
                if (declaredField != null) {
                    z10 = true;
                }
            } catch (Throwable th) {
                Logger.getLogger(sc2.class.getName()).logp(Level.WARNING, "com.google.protobuf.UnsafeUtil", "logMissingMethod", "platform method missing - proto runtime falling back to safer methods: ".concat(th.toString()));
            }
        }
        f16408d = z10;
        rc2 rc2Var2 = f16407c;
        if (rc2Var2 == null) {
            z11 = false;
        } else {
            try {
                Class<?> cls3 = ((Unsafe) rc2Var2.f16102a).getClass();
                cls3.getMethod("objectFieldOffset", Field.class);
                cls3.getMethod("arrayBaseOffset", Class.class);
                cls3.getMethod("arrayIndexScale", Class.class);
                Class<?> cls4 = Long.TYPE;
                cls3.getMethod("getInt", Object.class, cls4);
                cls3.getMethod("putInt", Object.class, cls4, Integer.TYPE);
                cls3.getMethod("getLong", Object.class, cls4);
                cls3.getMethod("putLong", Object.class, cls4, cls4);
                cls3.getMethod("getObject", Object.class, cls4);
                cls3.getMethod("putObject", Object.class, cls4, Object.class);
                z11 = true;
            } catch (Throwable th2) {
                Logger.getLogger(sc2.class.getName()).logp(Level.WARNING, "com.google.protobuf.UnsafeUtil", "logMissingMethod", "platform method missing - proto runtime falling back to safer methods: ".concat(th2.toString()));
                z11 = false;
            }
        }
        f16409e = z11;
        f16410f = a(byte[].class);
        a(boolean[].class);
        b(boolean[].class);
        a(int[].class);
        b(int[].class);
        a(long[].class);
        b(long[].class);
        a(float[].class);
        b(float[].class);
        a(double[].class);
        b(double[].class);
        a(Object[].class);
        b(Object[].class);
        int i11 = fa2.f11138a;
        try {
            declaredField2 = Buffer.class.getDeclaredField("effectiveDirectAddress");
        } catch (Throwable unused3) {
            declaredField2 = null;
        }
        if (declaredField2 == null) {
            try {
                declaredField2 = Buffer.class.getDeclaredField("address");
            } catch (Throwable unused4) {
                declaredField2 = null;
            }
            if (declaredField2 == null || declaredField2.getType() != Long.TYPE) {
                declaredField2 = null;
            }
        }
        long jObjectFieldOffset = -1;
        if (declaredField2 != null && (rc2Var = f16407c) != null) {
            jObjectFieldOffset = ((Unsafe) rc2Var.f16102a).objectFieldOffset(declaredField2);
        }
        f16411g = jObjectFieldOffset;
        f16412h = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
    }

    public static int a(Class cls) {
        if (f16409e) {
            return ((Unsafe) f16407c.f16102a).arrayBaseOffset(cls);
        }
        return -1;
    }

    public static void b(Class cls) {
        if (f16409e) {
            ((Unsafe) f16407c.f16102a).arrayIndexScale(cls);
        }
    }

    public static void c(Object obj, long j10, byte b10) {
        Unsafe unsafe = (Unsafe) f16407c.f16102a;
        long j11 = (-4) & j10;
        int i10 = unsafe.getInt(obj, j11);
        int i11 = ((~((int) j10)) & 3) << 3;
        unsafe.putInt(obj, j11, ((255 & b10) << i11) | (i10 & (~(255 << i11))));
    }

    public static void d(Object obj, long j10, byte b10) {
        Unsafe unsafe = (Unsafe) f16407c.f16102a;
        long j11 = (-4) & j10;
        int i10 = (((int) j10) & 3) << 3;
        unsafe.putInt(obj, j11, ((255 & b10) << i10) | (unsafe.getInt(obj, j11) & (~(255 << i10))));
    }

    public static Object e(Class cls) {
        try {
            return f16405a.allocateInstance(cls);
        } catch (InstantiationException e10) {
            throw new IllegalStateException(e10);
        }
    }

    public static int f(long j10, Object obj) {
        return ((Unsafe) f16407c.f16102a).getInt(obj, j10);
    }

    public static void g(int i10, long j10, Object obj) {
        ((Unsafe) f16407c.f16102a).putInt(obj, j10, i10);
    }

    public static long h(long j10, Object obj) {
        return ((Unsafe) f16407c.f16102a).getLong(obj, j10);
    }

    public static void i(Object obj, long j10, long j11) {
        ((Unsafe) f16407c.f16102a).putLong(obj, j10, j11);
    }

    public static boolean j(long j10, Object obj) {
        return f16407c.y0(j10, obj);
    }

    public static void k(Object obj, long j10, boolean z10) {
        f16407c.z0(obj, j10, z10);
    }

    public static float l(long j10, Object obj) {
        return f16407c.A0(j10, obj);
    }

    public static void m(Object obj, long j10, float f10) {
        f16407c.C0(obj, j10, f10);
    }

    public static double n(long j10, Object obj) {
        return f16407c.D0(j10, obj);
    }

    public static void o(Object obj, long j10, double d10) {
        f16407c.E0(obj, j10, d10);
    }

    public static Object p(long j10, Object obj) {
        return ((Unsafe) f16407c.f16102a).getObject(obj, j10);
    }

    public static void q(Object obj, long j10, Object obj2) {
        ((Unsafe) f16407c.f16102a).putObject(obj, j10, obj2);
    }

    public static void r(byte[] bArr, long j10, byte b10) {
        f16407c.s(bArr, f16410f + j10, b10);
    }

    public static long s(ByteBuffer byteBuffer) {
        return ((Unsafe) f16407c.f16102a).getLong(byteBuffer, f16411g);
    }

    public static Unsafe t() {
        try {
            return (Unsafe) AccessController.doPrivileged(new oc2());
        } catch (Throwable unused) {
            return null;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static boolean u(Class cls) {
        int i10 = fa2.f11138a;
        try {
            Class cls2 = f16406b;
            Class cls3 = Boolean.TYPE;
            cls2.getMethod("peekLong", cls, cls3);
            cls2.getMethod("pokeLong", cls, Long.TYPE, cls3);
            Class cls4 = Integer.TYPE;
            cls2.getMethod("pokeInt", cls, cls4, cls3);
            cls2.getMethod("peekInt", cls, cls3);
            cls2.getMethod("pokeByte", cls, Byte.TYPE);
            cls2.getMethod("peekByte", cls);
            cls2.getMethod("pokeByteArray", cls, byte[].class, cls4, cls4);
            cls2.getMethod("peekByteArray", cls, byte[].class, cls4, cls4);
            return true;
        } catch (Throwable unused) {
            return false;
        }
    }

    public static /* synthetic */ boolean v(Object obj, long j10) {
        return ((byte) ((((Unsafe) f16407c.f16102a).getInt(obj, (-4) & j10) >>> ((int) (((~j10) & 3) << 3))) & 255)) != 0;
    }

    public static /* synthetic */ boolean w(long j10, Object obj) {
        return ((byte) ((((Unsafe) f16407c.f16102a).getInt(obj, (-4) & j10) >>> ((int) ((j10 & 3) << 3))) & 255)) != 0;
    }
}
