[tor-commits] [orbot/master] update code to properly unzip entries from APK zip if needed

n8fr8 at torproject.org n8fr8 at torproject.org
Mon May 14 19:34:41 UTC 2018


commit 4bdfb79a68845ce2677c40eadde36fd7e294d6ea
Author: n8fr8 <nathan at freitas.net>
Date:   Mon May 14 12:44:45 2018 -0400

    update code to properly unzip entries from APK zip if needed
---
 .../android/service/util/NativeLoader.java         | 114 +++++++++++++++++++++
 .../service/util/OtherResourceInstaller.java       |  29 ++++--
 2 files changed, 136 insertions(+), 7 deletions(-)

diff --git a/orbotservice/src/main/java/org/torproject/android/service/util/NativeLoader.java b/orbotservice/src/main/java/org/torproject/android/service/util/NativeLoader.java
new file mode 100644
index 00000000..d6a25436
--- /dev/null
+++ b/orbotservice/src/main/java/org/torproject/android/service/util/NativeLoader.java
@@ -0,0 +1,114 @@
+
+package org.torproject.android.service.util;
+
+import android.content.Context;
+import android.os.Build;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
+
+public class NativeLoader {
+
+    private final static String TAG = "TorNativeLoader";
+
+    private static boolean loadFromZip(Context context, String libName, File destLocalFile, String folder) {
+
+
+        ZipFile zipFile = null;
+        ZipInputStream stream = null;
+        try {
+            zipFile = new ZipFile(context.getApplicationInfo().sourceDir);
+            ZipEntry entry = zipFile.getEntry("lib/" + folder + "/" + libName + ".so");
+            if (entry == null) {
+                throw new Exception("Unable to find file in apk:" + "lib/" + folder + "/" + libName);
+            }
+            //the zip file entry is also zipped itself!
+            stream = new ZipInputStream(zipFile.getInputStream(entry));
+            stream.getNextEntry();
+
+            OutputStream out = new FileOutputStream(destLocalFile);
+            byte[] buf = new byte[4096];
+            int len;
+            while ((len = stream.read(buf)) > 0) {
+                Thread.yield();
+                out.write(buf, 0, len);
+            }
+            out.close();
+
+            if (Build.VERSION.SDK_INT >= 9) {
+                destLocalFile.setReadable(true, false);
+                destLocalFile.setExecutable(true, false);
+                destLocalFile.setWritable(true);
+            }
+
+
+            return true;
+        } catch (Exception e) {
+            Log.e(TAG, e.getMessage());
+        } finally {
+            if (stream != null) {
+                try {
+                    stream.close();
+                } catch (Exception e) {
+                    Log.e(TAG, e.getMessage());
+                }
+            }
+            if (zipFile != null) {
+                try {
+                    zipFile.close();
+                } catch (Exception e) {
+                    Log.e(TAG, e.getMessage());
+                }
+            }
+        }
+        return false;
+    }
+
+    public static synchronized boolean initNativeLibs(Context context, String binaryName, File destLocalFile) {
+
+        try {
+            String folder = null;
+
+            try {
+
+                if (Build.CPU_ABI.equalsIgnoreCase("armeabi-v7a")) {
+                    folder = "armeabi-v7a";
+                } else if (Build.CPU_ABI.startsWith("armeabi")) {
+                    folder = "armeabi";
+                } else if (Build.CPU_ABI.equalsIgnoreCase("x86")) {
+                    folder = "x86";
+                } else if (Build.CPU_ABI.equalsIgnoreCase("mips")) {
+                    folder = "mips";
+                } else {
+                    folder = "armeabi";
+                    //FileLog.e("tmessages", "Unsupported arch: " + Build.CPU_ABI);
+                }
+            } catch (Exception e) {
+                //  FileLog.e("tmessages", e);
+                Log.e(TAG, e.getMessage());
+                folder = "armeabi";
+            }
+
+
+            String javaArch = System.getProperty("os.arch");
+            if (javaArch != null && javaArch.contains("686")) {
+                folder = "x86";
+            }
+
+            if (loadFromZip(context, binaryName, destLocalFile, folder)) {
+                return true;
+            }
+
+        } catch (Throwable e) {
+            e.printStackTrace();
+        }
+
+        return false;
+    }
+}
diff --git a/orbotservice/src/main/java/org/torproject/android/service/util/OtherResourceInstaller.java b/orbotservice/src/main/java/org/torproject/android/service/util/OtherResourceInstaller.java
index 672d6411..c02c5c54 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/util/OtherResourceInstaller.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/util/OtherResourceInstaller.java
@@ -72,22 +72,37 @@ public class OtherResourceInstaller implements TorServiceConstants {
 
         File libBinary = new File(getNativeLibraryDir(context),OBFSCLIENT_ASSET_KEY + ".so");
         outFile = new File(installFolder, OBFSCLIENT_ASSET_KEY);
+        if (libBinary.exists()) {
+            if ((!outFile.exists()) || (libBinary.lastModified() > outFile.lastModified())) {
+                streamToFile(new FileInputStream(libBinary), outFile, false, true);
+            }
 
-        if ((!outFile.exists()) || (libBinary.lastModified() > outFile.lastModified())) {
-            streamToFile(new FileInputStream(libBinary), outFile, false, true);
+            setExecutable(outFile);
+        }
+        else
+        {
+            NativeLoader.initNativeLibs(context,OBFSCLIENT_ASSET_KEY,outFile);
         }
 
-        setExecutable(outFile);
+        if (!outFile.exists())
+            return false;
 
         libBinary = new File(getNativeLibraryDir(context),PDNSD_ASSET_KEY + ".so");
         outFile = new File(installFolder, PDNSD_ASSET_KEY);
+        if (libBinary.exists()) {
+            if ((!outFile.exists()) || (libBinary.lastModified() > outFile.lastModified())) {
+                streamToFile(new FileInputStream(libBinary), outFile, false, true);
+            }
 
-        if ((!outFile.exists()) || (libBinary.lastModified() > outFile.lastModified())) {
-            streamToFile(new FileInputStream(libBinary), outFile, false, true);
+            setExecutable(outFile);
+        }
+        else
+        {
+            NativeLoader.initNativeLibs(context,PDNSD_ASSET_KEY,outFile);
         }
 
-        setExecutable(outFile);
-
+        if (!outFile.exists())
+            return false;
 
         return true;
     }





More information about the tor-commits mailing list