|
| 1 | +package org.ffmpeg.android; |
| 2 | +/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */ |
| 3 | +/* See LICENSE for licensing information */ |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.DataInputStream; |
| 7 | +import java.io.DataOutputStream; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileNotFoundException; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.FileReader; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.io.OutputStream; |
| 15 | +import java.util.zip.GZIPInputStream; |
| 16 | +import java.util.zip.ZipEntry; |
| 17 | +import java.util.zip.ZipInputStream; |
| 18 | + |
| 19 | + |
| 20 | +import android.content.Context; |
| 21 | +import android.util.Log; |
| 22 | + |
| 23 | +public class BinaryInstaller { |
| 24 | + |
| 25 | + |
| 26 | + File installFolder; |
| 27 | + Context context; |
| 28 | + |
| 29 | + private static int isARMv6 = -1; |
| 30 | + private static String CHMOD_EXEC = "700"; |
| 31 | + |
| 32 | + private final static int FILE_WRITE_BUFFER_SIZE = 32256; |
| 33 | + |
| 34 | + public BinaryInstaller (Context context, File installFolder) |
| 35 | + { |
| 36 | + this.installFolder = installFolder; |
| 37 | + |
| 38 | + this.context = context; |
| 39 | + } |
| 40 | + |
| 41 | + // |
| 42 | + /* |
| 43 | + * Extract the Tor binary from the APK file using ZIP |
| 44 | + */ |
| 45 | + public boolean installFromRaw () throws IOException, FileNotFoundException |
| 46 | + { |
| 47 | + |
| 48 | + InputStream is; |
| 49 | + File outFile; |
| 50 | + |
| 51 | + is = context.getResources().openRawResource(R.raw.ffmpeg); |
| 52 | + outFile = new File(installFolder, "ffmpeg"); |
| 53 | + streamToFile(is, outFile, false, false, "700"); |
| 54 | + |
| 55 | + |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + private static void copyAssetFile(Context ctx, String asset, File file) throws IOException, InterruptedException |
| 61 | + { |
| 62 | + |
| 63 | + DataOutputStream out = new DataOutputStream(new FileOutputStream(file)); |
| 64 | + InputStream is = new GZIPInputStream(ctx.getAssets().open(asset)); |
| 65 | + |
| 66 | + byte buf[] = new byte[8172]; |
| 67 | + int len; |
| 68 | + while ((len = is.read(buf)) > 0) { |
| 69 | + out.write(buf, 0, len); |
| 70 | + } |
| 71 | + out.close(); |
| 72 | + is.close(); |
| 73 | + } |
| 74 | + /* |
| 75 | + * Write the inputstream contents to the file |
| 76 | + */ |
| 77 | + private static boolean streamToFile(InputStream stm, File outFile, boolean append, boolean zip, String mode) throws IOException |
| 78 | + |
| 79 | + { |
| 80 | + byte[] buffer = new byte[FILE_WRITE_BUFFER_SIZE]; |
| 81 | + |
| 82 | + int bytecount; |
| 83 | + |
| 84 | + |
| 85 | + OutputStream stmOut = new FileOutputStream(outFile, append); |
| 86 | + |
| 87 | + if (zip) |
| 88 | + { |
| 89 | + ZipInputStream zis = new ZipInputStream(stm); |
| 90 | + ZipEntry ze = zis.getNextEntry(); |
| 91 | + stm = zis; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + while ((bytecount = stm.read(buffer)) > 0) |
| 96 | + { |
| 97 | + |
| 98 | + stmOut.write(buffer, 0, bytecount); |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + stmOut.close(); |
| 103 | + stm.close(); |
| 104 | + |
| 105 | + Runtime.getRuntime().exec("chmod "+mode+" "+outFile.getAbsolutePath()); |
| 106 | + |
| 107 | + |
| 108 | + return true; |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + //copy the file from inputstream to File output - alternative impl |
| 113 | + public void copyFile (InputStream is, File outputFile) |
| 114 | + { |
| 115 | + |
| 116 | + try { |
| 117 | + outputFile.createNewFile(); |
| 118 | + DataOutputStream out = new DataOutputStream(new FileOutputStream(outputFile)); |
| 119 | + DataInputStream in = new DataInputStream(is); |
| 120 | + |
| 121 | + int b = -1; |
| 122 | + byte[] data = new byte[1024]; |
| 123 | + |
| 124 | + while ((b = in.read(data)) != -1) { |
| 125 | + out.write(data); |
| 126 | + } |
| 127 | + |
| 128 | + if (b == -1); //rejoice |
| 129 | + |
| 130 | + // |
| 131 | + out.flush(); |
| 132 | + out.close(); |
| 133 | + in.close(); |
| 134 | + // chmod? |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + } catch (IOException ex) { |
| 139 | + Log.e("ffmpeg", "error copying binary", ex); |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + /** |
| 147 | + * Check if this is an ARMv6 device |
| 148 | + * @return true if this is ARMv6 |
| 149 | + */ |
| 150 | + private static boolean isARMv6() { |
| 151 | + if (isARMv6 == -1) { |
| 152 | + BufferedReader r = null; |
| 153 | + try { |
| 154 | + isARMv6 = 0; |
| 155 | + r = new BufferedReader(new FileReader("/proc/cpuinfo")); |
| 156 | + for (String line = r.readLine(); line != null; line = r.readLine()) { |
| 157 | + if (line.startsWith("Processor") && line.contains("ARMv6")) { |
| 158 | + isARMv6 = 1; |
| 159 | + break; |
| 160 | + } else if (line.startsWith("CPU architecture") && (line.contains("6TE") || line.contains("5TE"))) { |
| 161 | + isARMv6 = 1; |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + } catch (Exception ex) { |
| 166 | + } finally { |
| 167 | + if (r != null) try {r.close();} catch (Exception ex) {} |
| 168 | + } |
| 169 | + } |
| 170 | + return (isARMv6 == 1); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Copies a raw resource file, given its ID to the given location |
| 175 | + * @param ctx context |
| 176 | + * @param resid resource id |
| 177 | + * @param file destination file |
| 178 | + * @param mode file permissions (E.g.: "755") |
| 179 | + * @throws IOException on error |
| 180 | + * @throws InterruptedException when interrupted |
| 181 | + */ |
| 182 | + private static void copyRawFile(Context ctx, int resid, File file, String mode, boolean isZipd) throws IOException, InterruptedException |
| 183 | + { |
| 184 | + final String abspath = file.getAbsolutePath(); |
| 185 | + // Write the iptables binary |
| 186 | + final FileOutputStream out = new FileOutputStream(file); |
| 187 | + InputStream is = ctx.getResources().openRawResource(resid); |
| 188 | + |
| 189 | + if (isZipd) |
| 190 | + { |
| 191 | + ZipInputStream zis = new ZipInputStream(is); |
| 192 | + ZipEntry ze = zis.getNextEntry(); |
| 193 | + is = zis; |
| 194 | + } |
| 195 | + |
| 196 | + byte buf[] = new byte[1024]; |
| 197 | + int len; |
| 198 | + while ((len = is.read(buf)) > 0) { |
| 199 | + out.write(buf, 0, len); |
| 200 | + } |
| 201 | + out.close(); |
| 202 | + is.close(); |
| 203 | + // Change the permissions |
| 204 | + Runtime.getRuntime().exec("chmod "+mode+" "+abspath).waitFor(); |
| 205 | + } |
| 206 | + |
| 207 | + |
| 208 | +} |
0 commit comments