2018-05-29 05:07:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.wireguard.android.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.zip.ZipEntry;
|
2018-06-19 11:37:31 +02:00
|
|
|
import java.util.zip.ZipFile;
|
2018-05-29 05:07:50 +02:00
|
|
|
|
|
|
|
public final class SharedLibraryLoader {
|
|
|
|
private static final String TAG = "WireGuard/" + SharedLibraryLoader.class.getSimpleName();
|
|
|
|
|
|
|
|
private SharedLibraryLoader() {
|
|
|
|
}
|
|
|
|
|
2018-06-02 10:08:36 +02:00
|
|
|
public static void loadSharedLibrary(final Context context, final String libName) {
|
2018-05-29 05:07:50 +02:00
|
|
|
Throwable noAbiException;
|
|
|
|
try {
|
|
|
|
System.loadLibrary(libName);
|
|
|
|
return;
|
|
|
|
} catch (UnsatisfiedLinkError e) {
|
|
|
|
Log.d(TAG, "Failed to load library normally, so attempting to extract from apk", e);
|
|
|
|
noAbiException = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
final ZipFile zipFile;
|
|
|
|
try {
|
|
|
|
zipFile = new ZipFile(new File(context.getApplicationInfo().sourceDir), ZipFile.OPEN_READ);
|
2018-06-02 10:08:36 +02:00
|
|
|
} catch (final IOException e) {
|
2018-05-29 05:07:50 +02:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
final String mappedLibName = System.mapLibraryName(libName);
|
2018-06-02 10:08:36 +02:00
|
|
|
final byte[] buffer = new byte[1024 * 32];
|
2018-05-29 05:07:50 +02:00
|
|
|
for (final String abi : Build.SUPPORTED_ABIS) {
|
|
|
|
final String libZipPath = "lib" + File.separatorChar + abi + File.separatorChar + mappedLibName;
|
|
|
|
final ZipEntry zipEntry = zipFile.getEntry(libZipPath);
|
|
|
|
if (zipEntry == null)
|
|
|
|
continue;
|
|
|
|
File f = null;
|
|
|
|
try {
|
|
|
|
f = File.createTempFile("lib", ".so", context.getCacheDir());
|
|
|
|
Log.d(TAG, "Extracting apk:/" + libZipPath + " to " + f.getAbsolutePath() + " and loading");
|
2018-06-02 10:08:36 +02:00
|
|
|
try (final FileOutputStream out = new FileOutputStream(f);
|
|
|
|
final InputStream in = zipFile.getInputStream(zipEntry)) {
|
|
|
|
int len;
|
|
|
|
while ((len = in.read(buffer)) != -1) {
|
|
|
|
out.write(buffer, 0, len);
|
|
|
|
}
|
2018-05-29 05:07:50 +02:00
|
|
|
}
|
|
|
|
System.load(f.getAbsolutePath());
|
|
|
|
return;
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(TAG, "Failed to load library apk:/" + libZipPath, e);
|
|
|
|
noAbiException = e;
|
|
|
|
} finally {
|
|
|
|
if (f != null)
|
2018-06-02 10:08:36 +02:00
|
|
|
// noinspection ResultOfMethodCallIgnored
|
2018-05-29 05:07:50 +02:00
|
|
|
f.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (noAbiException instanceof RuntimeException)
|
|
|
|
throw (RuntimeException) noAbiException;
|
|
|
|
throw new RuntimeException(noAbiException);
|
|
|
|
}
|
|
|
|
}
|