package io.netty.util.internal;

import com.seewo.sdk.model.SDKKeyboardCode;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import sun.misc.Unsafe;

/* JADX INFO: loaded from: classes.dex */
final class PlatformDependent0 {
    private static final long ADDRESS_FIELD_OFFSET;
    private static final long BYTE_ARRAY_BASE_OFFSET;
    private static final Constructor<?> DIRECT_BUFFER_CONSTRUCTOR;
    static final int HASH_CODE_ASCII_SEED = -1028477387;
    static final int HASH_CODE_C1 = 461845907;
    static final int HASH_CODE_C2 = 461845907;
    private static final boolean UNALIGNED;
    static final Unsafe UNSAFE;
    private static final long UNSAFE_COPY_THRESHOLD = 1048576;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) PlatformDependent0.class);

    static {
        Field declaredField;
        Unsafe unsafe;
        long jAllocateMemory;
        Constructor<?> declaredConstructor;
        boolean zMatches;
        ByteBuffer byteBufferAllocateDirect = ByteBuffer.allocateDirect(1);
        try {
            declaredField = Buffer.class.getDeclaredField("address");
            declaredField.setAccessible(true);
        } catch (Throwable unused) {
        }
        if (declaredField.getLong(byteBufferAllocateDirect) == 0) {
            declaredField = null;
        }
        InternalLogger internalLogger = logger;
        internalLogger.debug("java.nio.Buffer.address: {}", declaredField != null ? "available" : "unavailable");
        if (declaredField != null) {
            try {
                Field declaredField2 = Unsafe.class.getDeclaredField("theUnsafe");
                declaredField2.setAccessible(true);
                unsafe = (Unsafe) declaredField2.get(null);
                internalLogger.debug("sun.misc.Unsafe.theUnsafe: {}", unsafe != null ? "available" : "unavailable");
                if (unsafe != null) {
                    try {
                        Class<?> cls = unsafe.getClass();
                        Class<?> cls2 = Long.TYPE;
                        cls.getDeclaredMethod("copyMemory", Object.class, cls2, Object.class, cls2, cls2);
                        internalLogger.debug("sun.misc.Unsafe.copyMemory: available");
                    } catch (NoSuchMethodError e2) {
                        logger.debug("sun.misc.Unsafe.copyMemory: unavailable");
                        throw e2;
                    } catch (NoSuchMethodException e3) {
                        logger.debug("sun.misc.Unsafe.copyMemory: unavailable");
                        throw e3;
                    }
                }
            } catch (Throwable unused2) {
                unsafe = null;
            }
        } else {
            unsafe = null;
        }
        UNSAFE = unsafe;
        if (unsafe == null) {
            ADDRESS_FIELD_OFFSET = -1L;
            BYTE_ARRAY_BASE_OFFSET = -1L;
            UNALIGNED = false;
            DIRECT_BUFFER_CONSTRUCTOR = null;
        } else {
            try {
                declaredConstructor = byteBufferAllocateDirect.getClass().getDeclaredConstructor(Long.TYPE, Integer.TYPE);
                declaredConstructor.setAccessible(true);
                jAllocateMemory = unsafe.allocateMemory(1L);
            } catch (Throwable unused3) {
                jAllocateMemory = -1;
            }
            try {
                declaredConstructor.newInstance(Long.valueOf(jAllocateMemory), 1);
                if (jAllocateMemory != -1) {
                    unsafe.freeMemory(jAllocateMemory);
                }
            } catch (Throwable unused4) {
                if (jAllocateMemory != -1) {
                    UNSAFE.freeMemory(jAllocateMemory);
                }
                declaredConstructor = null;
            }
            DIRECT_BUFFER_CONSTRUCTOR = declaredConstructor;
            ADDRESS_FIELD_OFFSET = objectFieldOffset(declaredField);
            BYTE_ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
            try {
                Method declaredMethod = Class.forName("java.nio.Bits", false, ClassLoader.getSystemClassLoader()).getDeclaredMethod("unaligned", new Class[0]);
                declaredMethod.setAccessible(true);
                zMatches = Boolean.TRUE.equals(declaredMethod.invoke(null, new Object[0]));
            } catch (Throwable unused5) {
                zMatches = SystemPropertyUtil.get("os.arch", "").matches("^(i[3-6]86|x86(_64)?|x64|amd64)$");
            }
            UNALIGNED = zMatches;
            logger.debug("java.nio.Bits.unaligned: {}", Boolean.valueOf(zMatches));
        }
        logger.debug("java.nio.DirectByteBuffer.<init>(long, int): {}", DIRECT_BUFFER_CONSTRUCTOR == null ? "unavailable" : "available");
    }

    private PlatformDependent0() {
    }

    static int addressSize() {
        return UNSAFE.addressSize();
    }

    static ByteBuffer allocateDirectNoCleaner(int i) {
        return newDirectBuffer(UNSAFE.allocateMemory(i), i);
    }

    static long allocateMemory(long j) {
        return UNSAFE.allocateMemory(j);
    }

    static long byteArrayBaseOffset() {
        return BYTE_ARRAY_BASE_OFFSET;
    }

    static void copyMemory(long j, long j2, long j3) {
        while (j3 > 0) {
            long jMin = Math.min(j3, UNSAFE_COPY_THRESHOLD);
            UNSAFE.copyMemory(j, j2, jMin);
            j3 -= jMin;
            j += jMin;
            j2 += jMin;
        }
    }

    static long directBufferAddress(ByteBuffer byteBuffer) {
        return getLong(byteBuffer, ADDRESS_FIELD_OFFSET);
    }

    static boolean equals(byte[] bArr, int i, byte[] bArr2, int i2, int i3) {
        long j = BYTE_ARRAY_BASE_OFFSET;
        long j2 = ((long) i) + j;
        long j3 = j + ((long) i2);
        int i4 = i3 & 7;
        long j4 = ((long) i4) + j2;
        long j5 = i3;
        long j6 = (j2 - 8) + j5;
        long j7 = (j3 - 8) + j5;
        while (j6 >= j4) {
            Unsafe unsafe = UNSAFE;
            if (unsafe.getLong(bArr, j6) != unsafe.getLong(bArr2, j7)) {
                return false;
            }
            j6 -= 8;
            j7 -= 8;
        }
        switch (i4) {
            case 1:
                Unsafe unsafe2 = UNSAFE;
                return unsafe2.getByte(bArr, j2) == unsafe2.getByte(bArr2, j3);
            case 2:
                Unsafe unsafe3 = UNSAFE;
                return unsafe3.getChar(bArr, j2) == unsafe3.getChar(bArr2, j3);
            case 3:
                Unsafe unsafe4 = UNSAFE;
                return unsafe4.getChar(bArr, j2 + 1) == unsafe4.getChar(bArr2, 1 + j3) && unsafe4.getByte(bArr, j2) == unsafe4.getByte(bArr2, j3);
            case 4:
                Unsafe unsafe5 = UNSAFE;
                return unsafe5.getInt(bArr, j2) == unsafe5.getInt(bArr2, j3);
            case 5:
                Unsafe unsafe6 = UNSAFE;
                return unsafe6.getInt(bArr, j2 + 1) == unsafe6.getInt(bArr2, 1 + j3) && unsafe6.getByte(bArr, j2) == unsafe6.getByte(bArr2, j3);
            case 6:
                Unsafe unsafe7 = UNSAFE;
                return unsafe7.getInt(bArr, j2 + 2) == unsafe7.getInt(bArr2, 2 + j3) && unsafe7.getChar(bArr, j2) == unsafe7.getChar(bArr2, j3);
            case 7:
                Unsafe unsafe8 = UNSAFE;
                return unsafe8.getInt(bArr, j2 + 3) == unsafe8.getInt(bArr2, 3 + j3) && unsafe8.getChar(bArr, j2 + 1) == unsafe8.getChar(bArr2, 1 + j3) && unsafe8.getByte(bArr, j2) == unsafe8.getByte(bArr2, j3);
            default:
                return true;
        }
    }

    static int equalsConstantTime(byte[] bArr, int i, byte[] bArr2, int i2, int i3) {
        long j = BYTE_ARRAY_BASE_OFFSET;
        long j2 = ((long) i) + j;
        long j3 = j + ((long) i2);
        int i4 = i3 & 7;
        long j4 = ((long) i4) + j2;
        long j5 = i3;
        long j6 = (j2 - 8) + j5;
        long j7 = (j3 - 8) + j5;
        long j8 = 0;
        while (j6 >= j4) {
            Unsafe unsafe = UNSAFE;
            j8 |= unsafe.getLong(bArr, j6) ^ unsafe.getLong(bArr2, j7);
            j6 -= 8;
            j7 -= 8;
        }
        switch (i4) {
            case 1:
                Unsafe unsafe2 = UNSAFE;
                return ConstantTimeUtils.equalsConstantTime(((long) (unsafe2.getByte(bArr, j2) ^ unsafe2.getByte(bArr2, j3))) | j8, 0L);
            case 2:
                Unsafe unsafe3 = UNSAFE;
                return ConstantTimeUtils.equalsConstantTime(((long) (unsafe3.getChar(bArr, j2) ^ unsafe3.getChar(bArr2, j3))) | j8, 0L);
            case 3:
                Unsafe unsafe4 = UNSAFE;
                return ConstantTimeUtils.equalsConstantTime(((long) (unsafe4.getByte(bArr, j2) ^ unsafe4.getByte(bArr2, j3))) | ((long) (unsafe4.getChar(bArr, j2 + 1) ^ unsafe4.getChar(bArr2, 1 + j3))) | j8, 0L);
            case 4:
                Unsafe unsafe5 = UNSAFE;
                return ConstantTimeUtils.equalsConstantTime(((long) (unsafe5.getInt(bArr, j2) ^ unsafe5.getInt(bArr2, j3))) | j8, 0L);
            case 5:
                Unsafe unsafe6 = UNSAFE;
                return ConstantTimeUtils.equalsConstantTime(((long) (unsafe6.getByte(bArr, j2) ^ unsafe6.getByte(bArr2, j3))) | ((long) (unsafe6.getInt(bArr, j2 + 1) ^ unsafe6.getInt(bArr2, 1 + j3))) | j8, 0L);
            case 6:
                Unsafe unsafe7 = UNSAFE;
                return ConstantTimeUtils.equalsConstantTime(((long) (unsafe7.getChar(bArr, j2) ^ unsafe7.getChar(bArr2, j3))) | ((long) (unsafe7.getInt(bArr2, 2 + j3) ^ unsafe7.getInt(bArr, j2 + 2))) | j8, 0L);
            case 7:
                Unsafe unsafe8 = UNSAFE;
                return ConstantTimeUtils.equalsConstantTime(((long) (unsafe8.getByte(bArr, j2) ^ unsafe8.getByte(bArr2, j3))) | ((long) (unsafe8.getChar(bArr, j2 + 1) ^ unsafe8.getChar(bArr2, 1 + j3))) | j8 | ((long) (unsafe8.getInt(bArr, j2 + 3) ^ unsafe8.getInt(bArr2, 3 + j3))), 0L);
            default:
                return ConstantTimeUtils.equalsConstantTime(j8, 0L);
        }
    }

    static void freeDirectBuffer(ByteBuffer byteBuffer) {
        Cleaner0.freeDirectBuffer(byteBuffer);
    }

    static void freeMemory(long j) {
        UNSAFE.freeMemory(j);
    }

    static byte getByte(long j) {
        return UNSAFE.getByte(j);
    }

    static ClassLoader getClassLoader(final Class<?> cls) {
        return System.getSecurityManager() == null ? cls.getClassLoader() : (ClassLoader) AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { // from class: io.netty.util.internal.PlatformDependent0.1
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // java.security.PrivilegedAction
            public ClassLoader run() {
                return cls.getClassLoader();
            }
        });
    }

    static ClassLoader getContextClassLoader() {
        return System.getSecurityManager() == null ? Thread.currentThread().getContextClassLoader() : (ClassLoader) AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { // from class: io.netty.util.internal.PlatformDependent0.2
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // java.security.PrivilegedAction
            public ClassLoader run() {
                return Thread.currentThread().getContextClassLoader();
            }
        });
    }

    static int getInt(Object obj, long j) {
        return UNSAFE.getInt(obj, j);
    }

    private static long getLong(Object obj, long j) {
        return UNSAFE.getLong(obj, j);
    }

    static Object getObject(Object obj, long j) {
        return UNSAFE.getObject(obj, j);
    }

    static Object getObjectVolatile(Object obj, long j) {
        return UNSAFE.getObjectVolatile(obj, j);
    }

    static short getShort(long j) {
        return UNSAFE.getShort(j);
    }

    static ClassLoader getSystemClassLoader() {
        return System.getSecurityManager() == null ? ClassLoader.getSystemClassLoader() : (ClassLoader) AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { // from class: io.netty.util.internal.PlatformDependent0.3
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // java.security.PrivilegedAction
            public ClassLoader run() {
                return ClassLoader.getSystemClassLoader();
            }
        });
    }

    static boolean hasDirectBufferNoCleanerConstructor() {
        return DIRECT_BUFFER_CONSTRUCTOR != null;
    }

    static boolean hasUnsafe() {
        return UNSAFE != null;
    }

    static int hashCodeAscii(byte[] bArr, int i, int i2) {
        int iHashCodeAsciiSanitize;
        int iHashCodeAsciiSanitize2;
        long j = BYTE_ARRAY_BASE_OFFSET + ((long) i);
        int i3 = i2 & 7;
        long j2 = ((long) i3) + j;
        int iHashCodeAsciiCompute = HASH_CODE_ASCII_SEED;
        for (long j3 = (j - 8) + ((long) i2); j3 >= j2; j3 -= 8) {
            iHashCodeAsciiCompute = hashCodeAsciiCompute(UNSAFE.getLong(bArr, j3), iHashCodeAsciiCompute);
        }
        switch (i3) {
            case 1:
                iHashCodeAsciiSanitize = iHashCodeAsciiCompute * 461845907;
                iHashCodeAsciiSanitize2 = hashCodeAsciiSanitize(UNSAFE.getByte(bArr, j));
                break;
            case 2:
                iHashCodeAsciiSanitize = iHashCodeAsciiCompute * 461845907;
                iHashCodeAsciiSanitize2 = hashCodeAsciiSanitize(UNSAFE.getShort(bArr, j));
                break;
            case 3:
                Unsafe unsafe = UNSAFE;
                iHashCodeAsciiSanitize = ((iHashCodeAsciiCompute * 461845907) + hashCodeAsciiSanitize(unsafe.getByte(bArr, j))) * 461845907;
                iHashCodeAsciiSanitize2 = hashCodeAsciiSanitize(unsafe.getShort(bArr, j + 1));
                break;
            case 4:
                iHashCodeAsciiSanitize = iHashCodeAsciiCompute * 461845907;
                iHashCodeAsciiSanitize2 = hashCodeAsciiSanitize(UNSAFE.getInt(bArr, j));
                break;
            case 5:
                Unsafe unsafe2 = UNSAFE;
                iHashCodeAsciiSanitize = ((iHashCodeAsciiCompute * 461845907) + hashCodeAsciiSanitize(unsafe2.getByte(bArr, j))) * 461845907;
                iHashCodeAsciiSanitize2 = hashCodeAsciiSanitize(unsafe2.getInt(bArr, j + 1));
                break;
            case 6:
                Unsafe unsafe3 = UNSAFE;
                iHashCodeAsciiSanitize = ((iHashCodeAsciiCompute * 461845907) + hashCodeAsciiSanitize(unsafe3.getShort(bArr, j))) * 461845907;
                iHashCodeAsciiSanitize2 = hashCodeAsciiSanitize(unsafe3.getInt(bArr, j + 2));
                break;
            case 7:
                Unsafe unsafe4 = UNSAFE;
                iHashCodeAsciiSanitize = ((((iHashCodeAsciiCompute * 461845907) + hashCodeAsciiSanitize(unsafe4.getByte(bArr, j))) * 461845907) + hashCodeAsciiSanitize(unsafe4.getShort(bArr, 1 + j))) * 461845907;
                iHashCodeAsciiSanitize2 = hashCodeAsciiSanitize(unsafe4.getInt(bArr, j + 3));
                break;
            default:
                return iHashCodeAsciiCompute;
        }
        return iHashCodeAsciiSanitize + iHashCodeAsciiSanitize2;
    }

    static int hashCodeAsciiCompute(long j, int i) {
        return (i * 461845907) + (hashCodeAsciiSanitize((int) j) * 461845907) + ((int) ((j & 2242545357458243584L) >>> 32));
    }

    static int hashCodeAsciiSanitize(byte b2) {
        return b2 & SDKKeyboardCode.FUNCTION_KEY_BOARD_2;
    }

    static int hashCodeAsciiSanitize(int i) {
        return i & 522133279;
    }

    static int hashCodeAsciiSanitize(short s) {
        return s & 7967;
    }

    static boolean isUnaligned() {
        return UNALIGNED;
    }

    static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(Class<? super T> cls, String str) throws Exception {
        return new UnsafeAtomicIntegerFieldUpdater(UNSAFE, cls, str);
    }

    static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater(Class<? super T> cls, String str) throws Exception {
        return new UnsafeAtomicLongFieldUpdater(UNSAFE, cls, str);
    }

    static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(Class<? super U> cls, String str) throws Exception {
        return new UnsafeAtomicReferenceFieldUpdater(UNSAFE, cls, str);
    }

    private static ByteBuffer newDirectBuffer(long j, int i) {
        try {
            return (ByteBuffer) DIRECT_BUFFER_CONSTRUCTOR.newInstance(Long.valueOf(j), Integer.valueOf(i));
        } catch (Throwable th) {
            if (th instanceof Error) {
                throw th;
            }
            throw new Error(th);
        }
    }

    static long objectFieldOffset(Field field) {
        return UNSAFE.objectFieldOffset(field);
    }

    static void putByte(long j, byte b2) {
        UNSAFE.putByte(j, b2);
    }

    static void putInt(long j, int i) {
        UNSAFE.putInt(j, i);
    }

    static void putLong(long j, long j2) {
        UNSAFE.putLong(j, j2);
    }

    static void putOrderedObject(Object obj, long j, Object obj2) {
        UNSAFE.putOrderedObject(obj, j, obj2);
    }

    static void putShort(long j, short s) {
        UNSAFE.putShort(j, s);
    }

    static ByteBuffer reallocateDirectNoCleaner(ByteBuffer byteBuffer, int i) {
        return newDirectBuffer(UNSAFE.reallocateMemory(directBufferAddress(byteBuffer), i), i);
    }

    static void setMemory(long j, long j2, byte b2) {
        UNSAFE.setMemory(j, j2, b2);
    }

    static void throwException(Throwable th) {
        UNSAFE.throwException((Throwable) ObjectUtil.checkNotNull(th, "cause"));
    }

    static boolean unalignedAccess() {
        return UNALIGNED;
    }

    static byte getByte(byte[] bArr, int i) {
        return UNSAFE.getByte(bArr, BYTE_ARRAY_BASE_OFFSET + ((long) i));
    }

    static int getInt(long j) {
        return UNSAFE.getInt(j);
    }

    static long getLong(long j) {
        return UNSAFE.getLong(j);
    }

    static short getShort(byte[] bArr, int i) {
        return UNSAFE.getShort(bArr, BYTE_ARRAY_BASE_OFFSET + ((long) i));
    }

    static void putByte(byte[] bArr, int i, byte b2) {
        UNSAFE.putByte(bArr, BYTE_ARRAY_BASE_OFFSET + ((long) i), b2);
    }

    static void putInt(byte[] bArr, int i, int i2) {
        UNSAFE.putInt(bArr, BYTE_ARRAY_BASE_OFFSET + ((long) i), i2);
    }

    static void putLong(byte[] bArr, int i, long j) {
        UNSAFE.putLong(bArr, BYTE_ARRAY_BASE_OFFSET + ((long) i), j);
    }

    static void putShort(byte[] bArr, int i, short s) {
        UNSAFE.putShort(bArr, BYTE_ARRAY_BASE_OFFSET + ((long) i), s);
    }

    static void setMemory(Object obj, long j, long j2, byte b2) {
        UNSAFE.setMemory(obj, j, j2, b2);
    }

    static void copyMemory(Object obj, long j, Object obj2, long j2, long j3) {
        long j4 = j;
        long j5 = j2;
        long j6 = j3;
        while (j6 > 0) {
            long jMin = Math.min(j6, UNSAFE_COPY_THRESHOLD);
            UNSAFE.copyMemory(obj, j4, obj2, j5, jMin);
            j6 -= jMin;
            j4 += jMin;
            j5 += jMin;
        }
    }

    static int getInt(byte[] bArr, int i) {
        return UNSAFE.getInt(bArr, BYTE_ARRAY_BASE_OFFSET + ((long) i));
    }

    static long getLong(byte[] bArr, int i) {
        return UNSAFE.getLong(bArr, BYTE_ARRAY_BASE_OFFSET + ((long) i));
    }
}
