TikTok.apk(点击下载) / Pine.java


package top.canyie.pine;

import android.util.Log;
import i.am.jaggu;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import top.canyie.pine.callback.MethodHook;

public final class Pine {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final int ARCH_ARM = 1;
    private static final int ARCH_ARM64 = 2;
    private static final int ARCH_X86 = 3;
    public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
    private static final String TAG = "Pine";
    private static int arch;
    private static volatile int hookMode = 0;
    private static volatile boolean initialized;
    private static final Map<String, Method> sBridgeMethods = new HashMap(8, 2.0f);
    private static HookHandler sHookHandler = new HookHandler() {
        /* class top.canyie.pine.Pine.AnonymousClass1 */

        @Override // top.canyie.pine.Pine.HookHandler
        public MethodHook.Unhook handleHook(HookRecord hookRecord, MethodHook methodHook, int i2, boolean z, boolean z2) {
            if (z) {
                Pine.hookNewMethod(hookRecord, i2, z2);
            }
            if (methodHook == null) {
                return null;
            }
            hookRecord.addCallback(methodHook);
            methodHook.getClass();
            return new MethodHook.Unhook(hookRecord);
        }

        @Override // top.canyie.pine.Pine.HookHandler
        public void handleUnhook(HookRecord hookRecord, MethodHook methodHook) {
            hookRecord.removeCallback(methodHook);
        }
    };
    private static HookListener sHookListener;
    private static final Object sHookLock = new Object();
    private static final Map<Long, HookRecord> sHookRecords = new ConcurrentHashMap();

    public static class CallFrame {
        public Object[] args;
        private HookRecord hookRecord;
        public final Member method;
        private Object result;
        boolean returnEarly;
        public Object thisObject;
        private Throwable throwable;

        public CallFrame(HookRecord hookRecord2, Object obj, Object[] objArr) {
            this.hookRecord = hookRecord2;
            this.method = hookRecord2.target;
            this.thisObject = obj;
            this.args = objArr;
        }

        public Object getResult() {
            return this.result;
        }

        public Object getResultOrThrowable() throws Throwable {
            Throwable th = this.throwable;
            if (th == null) {
                return this.result;
            }
            throw th;
        }

        public Throwable getThrowable() {
            return this.throwable;
        }

        public boolean hasThrowable() {
            return this.throwable != null;
        }

        public Object invokeOriginalMethod() throws InvocationTargetException, IllegalAccessException {
            return Pine.callBackupMethod(this.hookRecord.target, this.hookRecord.backup, this.thisObject, this.args);
        }

        public Object invokeOriginalMethod(Object obj, Object... objArr) throws InvocationTargetException, IllegalAccessException {
            return Pine.callBackupMethod(this.hookRecord.target, this.hookRecord.backup, obj, objArr);
        }

        public boolean isReturnEarly() {
            return this.returnEarly;
        }

        public void resetResult() {
            this.result = null;
            this.throwable = null;
            this.returnEarly = false;
        }

        public void setResult(Object obj) {
            this.result = obj;
            this.throwable = null;
            this.returnEarly = true;
        }

        public void setThrowable(Throwable th) {
            this.throwable = th;
            this.result = null;
            this.returnEarly = true;
        }
    }

    public interface HookHandler {
        MethodHook.Unhook handleHook(HookRecord hookRecord, MethodHook methodHook, int i2, boolean z, boolean z2);

        void handleUnhook(HookRecord hookRecord, MethodHook methodHook);
    }

    public interface HookListener {
        void afterHook(Member member, MethodHook.Unhook unhook);

        void beforeHook(Member member, MethodHook methodHook);
    }

    public interface HookMode {
        public static final int AUTO = 0;
        public static final int INLINE = 1;
        public static final int REPLACEMENT = 2;
    }

    public static final class HookRecord {
        public final long artMethod;
        public Method backup;
        private Set<MethodHook> callbacks = new HashSet();
        public boolean isStatic;
        public int paramNumber;
        public Class<?>[] paramTypes;
        public final Member target;

        public HookRecord(Member member, long j) {
            this.target = member;
            this.artMethod = j;
        }

        public synchronized void addCallback(MethodHook methodHook) {
            this.callbacks.add(methodHook);
        }

        public synchronized boolean emptyCallbacks() {
            return this.callbacks.isEmpty();
        }

        public synchronized MethodHook[] getCallbacks() {
            return (MethodHook[]) this.callbacks.toArray(new MethodHook[this.callbacks.size()]);
        }

        public boolean isPending() {
            return this.backup == null;
        }

        public synchronized void removeCallback(MethodHook methodHook) {
            this.callbacks.remove(methodHook);
        }
    }

    public interface LibLoader {
        void loadLib();
    }

    private Pine() {
        throw new RuntimeException(jaggu.base.decode("36150016441002445153425F04115C095747"));
    }

    static Object callBackupMethod(Member member, Method method, Object obj, Object[] objArr) throws InvocationTargetException, IllegalAccessException {
        if (PineConfig.sdkLevel < 24) {
            return method.invoke(obj, objArr);
        }
        Class<?> declaringClass = member.getDeclaringClass();
        updateDeclaringClass(member, method);
        Object invoke = method.invoke(obj, objArr);
        declaringClass.getClass();
        return invoke;
    }

    public static native long cloneExtras(long j);

    public static boolean compile(Member member) {
        int modifiers = member.getModifiers();
        Class<?> declaringClass = member.getDeclaringClass();
        if (!(member instanceof Method) && !(member instanceof Constructor)) {
            throw new IllegalArgumentException(jaggu.base.decode("2C08094F17090644505F064141045A0213570E591242161050440C4045135A510D46075317070C5D48590E57055F14") + member);
        } else if (Modifier.isAbstract(modifiers)) {
            throw new IllegalArgumentException(jaggu.base.decode("20070B5858104353575D125B0D00140751471545005510455E55175A59574A0A43") + member);
        } else if (Modifier.isNative(modifiers) || Proxy.isProxyClass(declaringClass)) {
            return false;
        } else {
            ensureInitialized();
            return compile0(currentArtThread0(), member);
        }
    }

    private static native boolean compile0(long j, Member member);

    public static native long currentArtThread0();

    public static boolean decompile(Member member, boolean z) {
        int modifiers = member.getModifiers();
        Class<?> declaringClass = member.getDeclaringClass();
        if (!(member instanceof Method) && !(member instanceof Constructor)) {
            throw new IllegalArgumentException(jaggu.base.decode("2C08094F17090644505F064141045A0213570E591242161050440C4045135A510D46075317000653575D125B0D00505C13") + member);
        } else if (Modifier.isAbstract(modifiers)) {
            throw new IllegalArgumentException(jaggu.base.decode("20070B58581043545D530D5F110C5803135503441544050647100E57425B5654105C45") + member);
        } else if (Proxy.isProxyClass(declaringClass)) {
            return false;
        } else {
            ensureInitialized();
            return decompile0(member, z);
        }
    }

    private static native boolean decompile0(Member member, boolean z);

    public static void disableHiddenApiPolicy(boolean z, boolean z2) {
        if (initialized) {
            disableHiddenApiPolicy0(z, z2);
            return;
        }
        PineConfig.disableHiddenApiPolicy = z;
        PineConfig.disableHiddenApiPolicyForPlatformDomain = z2;
        ensureInitialized();
    }

    private static native void disableHiddenApiPolicy0(boolean z, boolean z2);

    public static boolean disableJitInline() {
        if (PineConfig.sdkLevel < 24) {
            return false;
        }
        ensureInitialized();
        return disableJitInline0();
    }

    private static native boolean disableJitInline0();

    public static boolean disableProfileSaver() {
        if (PineConfig.sdkLevel < 24) {
            return false;
        }
        ensureInitialized();
        return disableProfileSaver0();
    }

    private static native boolean disableProfileSaver0();

    private static native void enableFastNative();

    public static void ensureInitialized() {
        if (!initialized) {
            synchronized (Pine.class) {
                if (!initialized) {
                    initialize();
                    initialized = true;
                }
            }
        }
    }

    public static long getAddress(long j, Object obj) {
        if (obj == null) {
            return 0;
        }
        return getAddress0(j, obj);
    }

    private static native long getAddress0(long j, Object obj);

    public static native void getArgsArm32(int i2, int i3, int[] iArr, int[] iArr2, float[] fArr);

    public static native void getArgsArm64(long j, long j2, boolean[] zArr, long[] jArr, long[] jArr2, double[] dArr);

    public static native void getArgsX86(int i2, int[] iArr, int i3);

    private static native long getArtMethod(Member member);

    public static HookHandler getHookHandler() {
        return sHookHandler;
    }

    public static HookListener getHookListener() {
        return sHookListener;
    }

    public static HookRecord getHookRecord(long j) {
        HookRecord hookRecord = sHookRecords.get(Long.valueOf(j));
        if (hookRecord != null) {
            return hookRecord;
        }
        throw new AssertionError(jaggu.base.decode("2D09457E580B08625D530D4005455209465A0517075916457242177F5347515F074615595E0A17554A10524A") + Long.toHexString(j));
    }

    public static Object getObject(long j, long j2) {
        if (j2 == 0) {
            return null;
        }
        return getObject0(j, j2);
    }

    private static native Object getObject0(long j, long j2);

    public static Object handleCall(HookRecord hookRecord, Object obj, Object[] objArr) throws Throwable {
        String decode = jaggu.base.decode("3608004E470100445D54425719065116475D0E59415907064642115752134E580608455556080F59565742");
        String decode2 = jaggu.base.decode("330F0B53");
        if (!PineConfig.debug || (!PineConfig.disableHooks && !hookRecord.emptyCallbacks())) {
            CallFrame callFrame = new CallFrame(hookRecord, obj, objArr);
            MethodHook[] callbacks = hookRecord.getCallbacks();
            int i2 = 0;
            while (true) {
                MethodHook methodHook = callbacks[i2];
                try {
                    methodHook.beforeCall(callFrame);
                    if (callFrame.returnEarly) {
                        i2++;
                        break;
                    }
                } catch (Throwable th) {
                    Log.e(decode2, decode + methodHook.getClass().getName() + jaggu.base.decode("4D04005058160673595C0E1A48"), th);
                    callFrame.resetResult();
                }
                i2++;
                if (i2 >= callbacks.length) {
                    break;
                }
            }
            if (!callFrame.returnEarly) {
                try {
                    callFrame.setResult(callFrame.invokeOriginalMethod());
                } catch (InvocationTargetException e) {
                    callFrame.setThrowable(e.getTargetException());
                } catch (Throwable th2) {
                    callFrame.setThrowable(th2);
                }
            }
            int i3 = i2 - 1;
            do {
                MethodHook methodHook2 = callbacks[i3];
                Object result = callFrame.getResult();
                Throwable throwable = callFrame.getThrowable();
                try {
                    methodHook2.afterCall(callFrame);
                } catch (Throwable th3) {
                    Log.e(decode2, decode + methodHook2.getClass().getName() + jaggu.base.decode("4D07034252162051545C4A1B"), th3);
                    if (throwable == null) {
                        callFrame.setResult(result);
                    } else {
                        callFrame.setThrowable(throwable);
                    }
                }
                i3--;
            } while (i3 >= 0);
            if (!callFrame.hasThrowable()) {
                return callFrame.getResult();
            }
            throw callFrame.getThrowable();
        }
        try {
            return callBackupMethod(hookRecord.target, hookRecord.backup, obj, objArr);
        } catch (InvocationTargetException e2) {
            throw e2.getTargetException();
        }
    }

    public static MethodHook.Unhook hook(Member member, MethodHook methodHook) {
        return hook(member, methodHook, true);
    }

    public static MethodHook.Unhook hook(Member member, MethodHook methodHook, boolean z) {
        HookRecord hookRecord;
        if (PineConfig.debug) {
            Log.d(jaggu.base.decode("330F0B53"), jaggu.base.decode("2B090A5D5E0A04105555165A0E0114") + member + jaggu.base.decode("43110C425F440051545C0053020E14") + methodHook);
        }
        if (member == null) {
            throw new NullPointerException(jaggu.base.decode("0E03115E5800430D05100C470D09"));
        } else if (methodHook != null) {
            int modifiers = member.getModifiers();
            boolean z2 = true;
            if (member instanceof Method) {
                if (!Modifier.isAbstract(modifiers)) {
                    ((Method) member).setAccessible(true);
                } else {
                    throw new IllegalArgumentException(jaggu.base.decode("20070B5858104358575F09120007471241550243415B01115B5F07410C13") + member);
                }
            } else if (!(member instanceof Constructor)) {
                throw new IllegalArgumentException(jaggu.base.decode("2C08094F17090644505F064141045A0213570E591242161050440C4045135A510D460753170C0C5F5355060841") + member);
            } else if (!Modifier.isStatic(modifiers)) {
                ((Constructor) member).setAccessible(true);
            } else {
                throw new IllegalArgumentException(jaggu.base.decode("20070B5858104358575F0912020955154014085908420D045F591957440919") + member);
            }
            ensureInitialized();
            HookListener hookListener = sHookListener;
            if (hookListener != null) {
                hookListener.beforeHook(member, methodHook);
            }
            long artMethod = getArtMethod(member);
            synchronized (sHookLock) {
                HookRecord hookRecord2 = sHookRecords.get(Long.valueOf(artMethod));
                if (hookRecord2 == null) {
                    HookRecord hookRecord3 = new HookRecord(member, artMethod);
                    sHookRecords.put(Long.valueOf(artMethod), hookRecord3);
                    hookRecord = hookRecord3;
                } else {
                    hookRecord = hookRecord2;
                    z2 = false;
                }
            }
            MethodHook.Unhook handleHook = sHookHandler.handleHook(hookRecord, methodHook, modifiers, z2, z);
            if (hookListener != null) {
                hookListener.afterHook(member, handleHook);
            }
            return handleHook;
        } else {
            throw new NullPointerException(jaggu.base.decode("0007095A5505005B180D5F120F10580A"));
        }
    }

    private static native Method hook0(long j, Class<?> cls, Member member, Method method, boolean z, boolean z2);

    static void hookNewMethod(HookRecord hookRecord, int i2, boolean z) {
        boolean z2;
        String str;
        Member member = hookRecord.target;
        boolean z3 = false;
        boolean z4 = hookMode != 0 ? hookMode == 1 : PineConfig.sdkLevel < 26;
        long currentArtThread0 = currentArtThread0();
        boolean isStatic = Modifier.isStatic(i2);
        hookRecord.isStatic = isStatic;
        if (isStatic && z) {
            resolve((Method) member);
            if (PineConfig.sdkLevel >= 30) {
                makeClassesVisiblyInitialized(currentArtThread0);
            }
        }
        Class<?> declaringClass = member.getDeclaringClass();
        boolean z5 = Modifier.isNative(i2) || Proxy.isProxyClass(declaringClass);
        if (!z4) {
            z2 = z4;
        } else if (!z5) {
            if (!compile0(currentArtThread0, member)) {
                Log.w(jaggu.base.decode("330F0B53"), jaggu.base.decode("20070B5858104353575D125B0D0014125B5141430044030047100E57425B56544F460359450706104A55125E0006510B565A15170C5900001D"));
            } else {
                z3 = z4;
            }
            z2 = z3;
        } else {
            z2 = false;
        }
        if (member instanceof Method) {
            Method method = (Method) member;
            hookRecord.paramTypes = method.getParameterTypes();
            Class<?> returnType = method.getReturnType();
            if (returnType.isPrimitive()) {
                str = returnType.getName() + jaggu.base.decode("21140C525001");
            } else {
                str = jaggu.base.decode("0C040F535410214251540557");
            }
        } else {
            hookRecord.paramTypes = ((Constructor) member).getParameterTypes();
            str = jaggu.base.decode("15090C5275160A545F55");
        }
        hookRecord.paramNumber = hookRecord.paramTypes.length;
        Method method2 = sBridgeMethods.get(str);
        if (method2 != null) {
            Method hook0 = hook0(currentArtThread0, declaringClass, member, method2, z2, z5);
            if (hook0 != null) {
                hook0.setAccessible(true);
                hookRecord.backup = hook0;
                return;
            }
            throw new RuntimeException(jaggu.base.decode("25070C5A5200434457100A5D0E0E140B564009580516") + member);
        }
        throw new AssertionError(jaggu.base.decode("20070B5858104356515E061203175D025451415A04420C0A5710055D4413") + member);
    }

    private static native void init0(int i2, boolean z, boolean z2, boolean z3, boolean z4, boolean z5);

    private static void initBridgeMethods() {
        Class<?>[] clsArr;
        String str;
        try {
            if (arch == 2) {
                str = jaggu.base.decode("1709151854050D4951554C42080B5148565A1545181825175E06577758474B49");
                clsArr = new Class[]{Long.TYPE, Long.TYPE, Long.TYPE, Long.TYPE, Long.TYPE, Long.TYPE, Long.TYPE};
            } else if (arch == 1) {
                str = jaggu.base.decode("1709151854050D4951554C42080B5148565A1545181825175E03517758474B49");
                clsArr = new Class[]{Integer.TYPE, Integer.TYPE, Integer.TYPE};
            } else if (arch == 3) {
                str = jaggu.base.decode("1709151854050D4951554C42080B5148565A154518183C5D05750D46444A");
                clsArr = new Class[]{Integer.TYPE, Integer.TYPE, Integer.TYPE};
            } else {
                throw new RuntimeException(jaggu.base.decode("3608004E470100445D54425313065C46") + arch);
            }
            Class<?> cls = Class.forName(str, true, Pine.class.getClassLoader());
            String[] strArr = {jaggu.base.decode("15090C5275160A545F55"), jaggu.base.decode("0A081174450D07575D"), jaggu.base.decode("0F090B5175160A545F55"), jaggu.base.decode("070910545B01214251540557"), jaggu.base.decode("050A0A57432611595C5707"), jaggu.base.decode("01090A5A52050D724A59065504"), jaggu.base.decode("011F115375160A545F55"), jaggu.base.decode("000E044475160A545F55"), jaggu.base.decode("100E0A44432611595C5707"), jaggu.base.decode("0C040F535410214251540557")};
            for (int i2 = 0; i2 < 10; i2++) {
                String str2 = strArr[i2];
                Method declaredMethod = cls.getDeclaredMethod(str2, clsArr);
                declaredMethod.setAccessible(true);
                sBridgeMethods.put(str2, declaredMethod);
            }
        } catch (Exception e) {
            throw new RuntimeException(jaggu.base.decode("25070C5A5200434457100B5C08111404415D05500416090047580C5645"), e);
        }
    }

    private static void initialize() {
        int i2 = PineConfig.sdkLevel;
        if (i2 >= 19) {
            if (i2 > 30) {
                Log.w(jaggu.base.decode("330F0B53"), jaggu.base.decode("22080144580D07104E551041080A5A46475B0E17095F030D1F100D5D42134D5510120052170A0C47161E4C"));
            }
            String property = System.getProperty(jaggu.base.decode("0907135719120E1E4E551041080A5A"));
            if (property == null || !property.startsWith(jaggu.base.decode("51"))) {
                throw new RuntimeException(jaggu.base.decode("2C08094F17171640485F104612457534671413420F420D0856"));
            }
            try {
                LibLoader libLoader = PineConfig.libLoader;
                if (libLoader != null) {
                    libLoader.loadLib();
                }
                init0(i2, PineConfig.debug, PineConfig.debuggable, PineConfig.antiChecks, PineConfig.disableHiddenApiPolicy, PineConfig.disableHiddenApiPolicyForPlatformDomain);
                initBridgeMethods();
                if (PineConfig.useFastNative && i2 >= 21) {
                    enableFastNative();
                }
            } catch (Exception e) {
                throw new RuntimeException(jaggu.base.decode("330F0B53170D0D594C100740130A46"), e);
            }
        } else {
            throw new RuntimeException(jaggu.base.decode("3608164347140C424C550612000B50145C5D051712520F455F5515575A13") + i2);
        }
    }

    public static Object invokeOriginalMethod(Member member, Object obj, Object... objArr) throws IllegalAccessException, InvocationTargetException {
        if (member != null) {
            if (member instanceof Method) {
                ((Method) member).setAccessible(true);
            } else if (member instanceof Constructor) {
                ((Constructor) member).setAccessible(true);
            } else {
                throw new IllegalArgumentException(jaggu.base.decode("0E03115E5800435D4D431612030014095514154E1153442856440B5D5213564243250A58441011455B440D40"));
            }
            HookRecord hookRecord = sHookRecords.get(Long.valueOf(getArtMethod(member)));
            if (hookRecord != null) {
                if (hookRecord.backup == null) {
                    resolve((Method) member);
                }
                return callBackupMethod(hookRecord.target, hookRecord.backup, obj, objArr);
            } else if (!(member instanceof Constructor)) {
                return ((Method) member).invoke(obj, objArr);
            } else {
                if (obj == null) {
                    try {
                        ((Constructor) member).newInstance(objArr);
                        return null;
                    } catch (InstantiationException e) {
                        throw new IllegalArgumentException(jaggu.base.decode("0A0813575B0D07107B5F0C4115174105475B13"), e);
                    }
                } else {
                    throw new IllegalArgumentException(jaggu.base.decode("20070B585810435956460D59044555465D5B151709590B0E56544371595D4A44111306425816434751440A1200455A095D190F420D5A44175653065B40564B"));
                }
            }
        } else {
            throw new NullPointerException(jaggu.base.decode("0E03115E5800430D05100C470D09"));
        }
    }

    public static boolean is64Bit() {
        ensureInitialized();
        return arch == 2;
    }

    public static boolean isHooked(Member member) {
        if ((member instanceof Method) || (member instanceof Constructor)) {
            return sHookRecords.containsKey(Long.valueOf(getArtMethod(member)));
        }
        throw new IllegalArgumentException(jaggu.base.decode("2C08094F17090644505F064141045A0213570E591242161050440C4045135A510D460753170C0C5F5355060841") + member);
    }

    public static boolean isInitialized() {
        return initialized;
    }

    public static void log(String str) {
        if (PineConfig.debug) {
            Log.i(jaggu.base.decode("330F0B53"), str);
        }
    }

    public static void log(String str, Object... objArr) {
        if (PineConfig.debug) {
            Log.i(jaggu.base.decode("330F0B53"), String.format(str, objArr));
        }
    }

    private static native void makeClassesVisiblyInitialized(long j);

    private static void resolve(Method method) {
        try {
            method.invoke(null, method.getParameterTypes().length > 0 ? null : new Object[1]);
            throw new RuntimeException(jaggu.base.decode("2D09457F5B080657595C2340061059035D40244F025314115A5F0D12425B4B5F140845415F010D104A55115D0D135146404000430855440856440B5D521D"));
        } catch (IllegalArgumentException unused) {
        } catch (Exception e) {
            throw new RuntimeException(jaggu.base.decode("36080E5858130D105D48015711115D095D14155F1359130B13470B5758134B55100909405244104459440B51410851125B5B0519"), e);
        }
    }

    public static void setDebuggable(boolean z) {
        if (!initialized) {
            synchronized (Pine.class) {
                if (!initialized) {
                    PineConfig.debuggable = z;
                    initialize();
                    initialized = true;
                    return;
                }
            }
        }
        PineConfig.debuggable = z;
        setDebuggable0(z);
    }

    private static native void setDebuggable0(boolean z);

    public static void setHookHandler(HookHandler hookHandler) {
        sHookHandler = hookHandler;
    }

    public static void setHookListener(HookListener hookListener) {
        sHookListener = hookListener;
    }

    public static void setHookMode(int i2) {
        if (i2 < 0 || i2 > 2) {
            throw new IllegalArgumentException(jaggu.base.decode("2A0A095350050F10505F0D592C0A500313") + i2);
        }
        hookMode = i2;
    }

    public static void setJitCompilationAllowed(boolean z) {
        if (PineConfig.sdkLevel >= 24) {
            ensureInitialized();
            setJitCompilationAllowed0(z);
        }
    }

    private static native void setJitCompilationAllowed0(boolean z);

    private static native void updateDeclaringClass(Member member, Method method);
}