Store CRC as apk file name

This commit is contained in:
Nullptr 2021-08-26 23:07:54 +08:00
parent 5f8159ec65
commit 113b2c93b8
2 changed files with 36 additions and 26 deletions

View File

@ -13,8 +13,6 @@ import android.util.Log;
import org.lsposed.lspatch.util.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
@ -39,24 +37,19 @@ public class LSPAppComponentFactoryStub extends AppComponentFactory {
* This method will be called at <b>instantiateClassLoader</b> by <b>createOrUpdateClassLoaderLocked</b>
**/
private void initOriginalAppComponentFactory(ApplicationInfo aInfo) {
final String cacheApkPath = aInfo.dataDir + "/cache/origin_apk.bin";
final String originPath = aInfo.dataDir + "/cache/lspatch/origin/";
final String originalAppComponentFactoryClass = FileUtils.readTextFromInputStream(baseClassLoader.getResourceAsStream(ORIGINAL_APP_COMPONENT_FACTORY_ASSET_PATH));
try {
File cacheApk = new File(cacheApkPath);
if (cacheApk.exists()) {
try (ZipFile sourceFile = new ZipFile(aInfo.sourceDir);
InputStream is = new FileInputStream(cacheApk)) {
var sourceCrc = sourceFile.getEntry(ORIGINAL_APK_ASSET_PATH).getCrc();
var cacheCrc = FileUtils.calculateCrc(is);
if (sourceCrc != cacheCrc) {
Log.i(TAG, "Application updated, extract original apk again");
cacheApk.delete();
}
}
String cacheApkPath;
try (ZipFile sourceFile = new ZipFile(aInfo.sourceDir)) {
cacheApkPath = originPath + sourceFile.getEntry(ORIGINAL_APK_ASSET_PATH).getCrc();
}
if (!cacheApk.exists()) {
if (!Files.exists(Paths.get(cacheApkPath))) {
Log.i(TAG, "extract original apk");
FileUtils.deleteFolderIfExists(Paths.get(originPath));
Files.createDirectories(Paths.get(originPath));
try (InputStream is = baseClassLoader.getResourceAsStream(ORIGINAL_APK_ASSET_PATH)) {
Files.copy(is, Paths.get(cacheApkPath));
}
@ -68,11 +61,11 @@ public class LSPAppComponentFactoryStub extends AppComponentFactory {
originalAppComponentFactory = (AppComponentFactory) appClassLoader.loadClass(originalAppComponentFactoryClass).newInstance();
} catch (ClassNotFoundException | NullPointerException ignored) {
if (originalAppComponentFactoryClass != null && !originalAppComponentFactoryClass.isEmpty())
Log.w(TAG, "Original AppComponentFactory not found");
Log.w(TAG, "original AppComponentFactory not found");
originalAppComponentFactory = new AppComponentFactory();
}
Log.d(TAG, "Instantiate original AppComponentFactory: " + originalAppComponentFactory);
Log.d(TAG, "instantiate original AppComponentFactory: " + originalAppComponentFactory);
} catch (Throwable e) {
Log.e(TAG, "initOriginalAppComponentFactory", e);
}

View File

@ -5,7 +5,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class FileUtils {
@ -22,13 +26,26 @@ public class FileUtils {
return null;
}
public static long calculateCrc(InputStream is) throws IOException {
CRC32 crcMaker = new CRC32();
byte[] buffer = new byte[65536];
int bytesRead;
while((bytesRead = is.read(buffer)) != -1) {
crcMaker.update(buffer, 0, bytesRead);
}
return crcMaker.getValue();
public static void deleteFolderIfExists(Path target) throws IOException {
if (Files.notExists(target)) return;
Files.walkFileTree(target, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException {
if (e == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
throw e;
}
}
});
}
}