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

import java.lang.reflect.Field;
import java.nio.Buffer;
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: classes.dex */
public abstract class H2 {

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

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

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

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

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

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

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

    /* JADX WARN: Removed duplicated region for block: B:11:0x004b  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x007b  */
    /* JADX WARN: Removed duplicated region for block: B:4:0x001e  */
    /* JADX WARN: Removed duplicated region for block: B:50:0x017c A[PHI: r0
      0x017c: PHI (r0v42 java.lang.reflect.Field) = (r0v35 java.lang.reflect.Field), (r0v37 java.lang.reflect.Field) binds: [B:43:0x016c, B:49:0x017a] A[DONT_GENERATE, DONT_INLINE]] */
    static {
        G2 f22;
        Field declaredField;
        boolean z8;
        boolean z9;
        Field declaredField2;
        Field field;
        G2 g22;
        Unsafe unsafeH = h();
        f9598a = unsafeH;
        int i = O1.f9656a;
        f9599b = Memory.class;
        Class<?> cls = Long.TYPE;
        boolean zN = n(cls);
        Class<?> cls2 = Integer.TYPE;
        boolean zN2 = n(cls2);
        if (unsafeH != null) {
            f22 = zN ? new F2(unsafeH) : zN2 ? new E2(unsafeH) : null;
        }
        f9600c = f22;
        if (f22 == null) {
            z8 = false;
        } else {
            try {
                Class<?> cls3 = f22.f9593a.getClass();
                cls3.getMethod("objectFieldOffset", Field.class);
                cls3.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() != cls) {
                            declaredField = null;
                        }
                    }
                }
                if (declaredField != null) {
                    z8 = true;
                }
            } catch (Throwable th) {
                Logger.getLogger(H2.class.getName()).logp(Level.WARNING, "com.google.protobuf.UnsafeUtil", "logMissingMethod", "platform method missing - proto runtime falling back to safer methods: ".concat(th.toString()));
            }
        }
        f9601d = z8;
        G2 g23 = f9600c;
        if (g23 == null) {
            z9 = false;
        } else {
            try {
                Class<?> cls4 = g23.f9593a.getClass();
                cls4.getMethod("objectFieldOffset", Field.class);
                cls4.getMethod("arrayBaseOffset", Class.class);
                cls4.getMethod("arrayIndexScale", Class.class);
                cls4.getMethod("getInt", Object.class, cls);
                cls4.getMethod("putInt", Object.class, cls, cls2);
                cls4.getMethod("getLong", Object.class, cls);
                cls4.getMethod("putLong", Object.class, cls, cls);
                cls4.getMethod("getObject", Object.class, cls);
                cls4.getMethod("putObject", Object.class, cls, Object.class);
                z9 = true;
            } catch (Throwable th2) {
                Logger.getLogger(H2.class.getName()).logp(Level.WARNING, "com.google.protobuf.UnsafeUtil", "logMissingMethod", "platform method missing - proto runtime falling back to safer methods: ".concat(th2.toString()));
                z9 = false;
            }
        }
        f9602e = z9;
        f9603f = o(byte[].class);
        o(boolean[].class);
        a(boolean[].class);
        o(int[].class);
        a(int[].class);
        o(long[].class);
        a(long[].class);
        o(float[].class);
        a(float[].class);
        o(double[].class);
        a(double[].class);
        o(Object[].class);
        a(Object[].class);
        int i5 = O1.f9656a;
        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;
            }
            field = (declaredField2 == null || declaredField2.getType() != cls) ? null : declaredField2;
        }
        if (field != null && (g22 = f9600c) != null) {
            g22.f9593a.objectFieldOffset(field);
        }
        f9604g = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
    }

    public static void a(Class cls) {
        if (f9602e) {
            f9600c.f9593a.arrayIndexScale(cls);
        }
    }

    public static void b(Object obj, long j8, byte b8) {
        Unsafe unsafe = f9600c.f9593a;
        long j9 = (-4) & j8;
        int i = unsafe.getInt(obj, j9);
        int i5 = ((~((int) j8)) & 3) << 3;
        unsafe.putInt(obj, j9, ((255 & b8) << i5) | (i & (~(255 << i5))));
    }

    public static void c(Object obj, long j8, byte b8) {
        Unsafe unsafe = f9600c.f9593a;
        long j9 = (-4) & j8;
        int i = (((int) j8) & 3) << 3;
        unsafe.putInt(obj, j9, ((255 & b8) << i) | (unsafe.getInt(obj, j9) & (~(255 << i))));
    }

    public static int d(long j8, Object obj) {
        return f9600c.f9593a.getInt(obj, j8);
    }

    public static long e(long j8, Object obj) {
        return f9600c.f9593a.getLong(obj, j8);
    }

    public static Object f(Class cls) {
        try {
            return f9598a.allocateInstance(cls);
        } catch (InstantiationException e8) {
            throw new IllegalStateException(e8);
        }
    }

    public static Object g(long j8, Object obj) {
        return f9600c.f9593a.getObject(obj, j8);
    }

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

    public static void i(int i, long j8, Object obj) {
        f9600c.f9593a.putInt(obj, j8, i);
    }

    public static void j(Object obj, long j8, long j9) {
        f9600c.f9593a.putLong(obj, j8, j9);
    }

    public static void k(Object obj, long j8, Object obj2) {
        f9600c.f9593a.putObject(obj, j8, obj2);
    }

    public static /* bridge */ /* synthetic */ boolean l(long j8, Object obj) {
        return ((byte) ((f9600c.f9593a.getInt(obj, (-4) & j8) >>> ((int) (((~j8) & 3) << 3))) & 255)) != 0;
    }

    public static /* bridge */ /* synthetic */ boolean m(long j8, Object obj) {
        return ((byte) ((f9600c.f9593a.getInt(obj, (-4) & j8) >>> ((int) ((j8 & 3) << 3))) & 255)) != 0;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static boolean n(Class cls) {
        int i = O1.f9656a;
        try {
            Class cls2 = f9599b;
            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 int o(Class cls) {
        if (f9602e) {
            return f9600c.f9593a.arrayBaseOffset(cls);
        }
        return -1;
    }
}
