OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import java.io.File; | 5 import java.io.File; |
6 import java.io.FileOutputStream; | 6 import java.io.FileOutputStream; |
7 import java.io.IOException; | 7 import java.io.IOException; |
8 import java.io.InputStream; | 8 import java.io.InputStream; |
9 import java.io.OutputStream; | 9 import java.io.OutputStream; |
10 import java.util.ArrayList; | 10 import java.util.ArrayList; |
(...skipping 21 matching lines...) Expand all Loading... |
32 class RezipApk { | 32 class RezipApk { |
33 // Alignment to use for non-compressed files (must match zipalign). | 33 // Alignment to use for non-compressed files (must match zipalign). |
34 private static final int ALIGNMENT = 4; | 34 private static final int ALIGNMENT = 4; |
35 | 35 |
36 // Alignment to use for non-compressed *.so files | 36 // Alignment to use for non-compressed *.so files |
37 private static final int LIBRARY_ALIGNMENT = 4096; | 37 private static final int LIBRARY_ALIGNMENT = 4096; |
38 | 38 |
39 // Files matching this pattern are not copied to the output when adding alig
nment. | 39 // Files matching this pattern are not copied to the output when adding alig
nment. |
40 // When reordering and verifying the APK they are copied to the end of the f
ile. | 40 // When reordering and verifying the APK they are copied to the end of the f
ile. |
41 private static Pattern sMetaFilePattern = | 41 private static Pattern sMetaFilePattern = |
42 Pattern.compile("^(META-INF/((.*)[.](SF|RSA|DSA)|com/android/otacert
))|(" + | 42 Pattern.compile("^(META-INF/((.*)[.](SF|RSA|DSA)|com/android/otacert
))|(" |
43 Pattern.quote(JarFile.MANIFEST_NAME) + ")$"); | 43 + Pattern.quote(JarFile.MANIFEST_NAME) + ")$"); |
44 | 44 |
45 // Pattern for matching a shared library in the APK | 45 // Pattern for matching a shared library in the APK |
46 private static Pattern sLibraryPattern = Pattern.compile("^lib/[^/]*/lib.*[.
]so$"); | 46 private static Pattern sLibraryPattern = Pattern.compile("^lib/[^/]*/lib.*[.
]so$"); |
47 // Pattern for match the crazy linker in the APK | 47 // Pattern for match the crazy linker in the APK |
48 private static Pattern sCrazyLinkerPattern = | 48 private static Pattern sCrazyLinkerPattern = |
49 Pattern.compile("^lib/[^/]*/libchromium_android_linker.so$"); | 49 Pattern.compile("^lib/[^/]*/libchromium_android_linker.so$"); |
50 // Pattern for matching a crazy loaded shared library in the APK | 50 // Pattern for matching a crazy loaded shared library in the APK |
51 private static Pattern sCrazyLibraryPattern = | 51 private static Pattern sCrazyLibraryPattern = Pattern.compile("^lib/[^/]*/cr
azy.lib.*[.]so$"); |
52 Pattern.compile("^lib/[^/]*/crazy.lib.*[.]so$"); | |
53 | 52 |
54 private static boolean isLibraryFilename(String filename) { | 53 private static boolean isLibraryFilename(String filename) { |
55 return sLibraryPattern.matcher(filename).matches() && | 54 return sLibraryPattern.matcher(filename).matches() |
56 !sCrazyLinkerPattern.matcher(filename).matches(); | 55 && !sCrazyLinkerPattern.matcher(filename).matches(); |
57 } | 56 } |
58 | 57 |
59 private static boolean isCrazyLibraryFilename(String filename) { | 58 private static boolean isCrazyLibraryFilename(String filename) { |
60 return sCrazyLibraryPattern.matcher(filename).matches(); | 59 return sCrazyLibraryPattern.matcher(filename).matches(); |
61 } | 60 } |
62 | 61 |
63 private static String renameLibraryForCrazyLinker(String filename) { | 62 private static String renameLibraryForCrazyLinker(String filename) { |
64 int lastSlash = filename.lastIndexOf('/'); | 63 int lastSlash = filename.lastIndexOf('/'); |
65 // We rename the library, so that the Android Package Manager | 64 // We rename the library, so that the Android Package Manager |
66 // no longer extracts the library. | 65 // no longer extracts the library. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 // is true do not include the jar entries for the META-INF files. | 142 // is true do not include the jar entries for the META-INF files. |
144 // Entries are ordered in the deterministic order used by SignApk. | 143 // Entries are ordered in the deterministic order used by SignApk. |
145 private static List<JarEntry> getOutputFileOrderEntries( | 144 private static List<JarEntry> getOutputFileOrderEntries( |
146 JarFile jar, boolean omitMetaFiles, boolean rename) { | 145 JarFile jar, boolean omitMetaFiles, boolean rename) { |
147 List<JarEntry> entries = new ArrayList<JarEntry>(); | 146 List<JarEntry> entries = new ArrayList<JarEntry>(); |
148 for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) { | 147 for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) { |
149 JarEntry entry = e.nextElement(); | 148 JarEntry entry = e.nextElement(); |
150 if (entry.isDirectory()) { | 149 if (entry.isDirectory()) { |
151 continue; | 150 continue; |
152 } | 151 } |
153 if (omitMetaFiles && | 152 if (omitMetaFiles && sMetaFilePattern.matcher(entry.getName()).match
es()) { |
154 sMetaFilePattern.matcher(entry.getName()).matches()) { | |
155 continue; | 153 continue; |
156 } | 154 } |
157 entries.add(entry); | 155 entries.add(entry); |
158 } | 156 } |
159 | 157 |
160 // We sort the input entries by name. When present META-INF files | 158 // We sort the input entries by name. When present META-INF files |
161 // are sorted to the end. | 159 // are sorted to the end. |
162 Collections.sort(entries, new EntryComparator(rename)); | 160 Collections.sort(entries, new EntryComparator(rename)); |
163 return entries; | 161 return entries; |
164 } | 162 } |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 out.closeEntry(); | 385 out.closeEntry(); |
388 out.flush(); | 386 out.flush(); |
389 prevName = name; | 387 prevName = name; |
390 } | 388 } |
391 if (numCrazy == 0) { | 389 if (numCrazy == 0) { |
392 throw new AssertionError("There was no crazy library in the archive"
); | 390 throw new AssertionError("There was no crazy library in the archive"
); |
393 } | 391 } |
394 } | 392 } |
395 | 393 |
396 private static void usage() { | 394 private static void usage() { |
397 System.err.println( | 395 System.err.println("Usage: prealignapk (addalignment|reorder) input.apk
output.apk"); |
398 "Usage: prealignapk (addalignment|reorder) input.apk output.apk"
); | 396 System.err.println("\"crazy\" libraries are always inflated in the outpu
t"); |
399 System.err.println( | |
400 "\"crazy\" libraries are always inflated in the output"); | |
401 System.err.println( | 397 System.err.println( |
402 " renamealign - rename libraries with \"crazy.\" prefix and ad
d alignment file"); | 398 " renamealign - rename libraries with \"crazy.\" prefix and ad
d alignment file"); |
403 System.err.println( | 399 System.err.println(" align - add alignment file"); |
404 " align - add alignment file"); | 400 System.err.println(" reorder - re-creates canonical ordering and c
hecks alignment"); |
405 System.err.println( | |
406 " reorder - re-creates canonical ordering and checks align
ment"); | |
407 System.exit(2); | 401 System.exit(2); |
408 } | 402 } |
409 | 403 |
410 public static void main(String[] args) throws IOException { | 404 public static void main(String[] args) throws IOException { |
411 if (args.length != 3) usage(); | 405 if (args.length != 3) usage(); |
412 | 406 |
413 boolean addAlignment = false; | 407 boolean addAlignment = false; |
414 boolean rename = false; | 408 boolean rename = false; |
415 if (args[0].equals("renamealign")) { | 409 if (args[0].equals("renamealign")) { |
416 // Normal case. Before signing we rename the library and add an alig
nment file. | 410 // Normal case. Before signing we rename the library and add an alig
nment file. |
(...skipping 28 matching lines...) Expand all Loading... |
445 outputJar.setLevel(9); | 439 outputJar.setLevel(9); |
446 | 440 |
447 rezip(inputJar, outputJar, outCount, addAlignment, rename); | 441 rezip(inputJar, outputJar, outCount, addAlignment, rename); |
448 outputJar.close(); | 442 outputJar.close(); |
449 } finally { | 443 } finally { |
450 if (inputJar != null) inputJar.close(); | 444 if (inputJar != null) inputJar.close(); |
451 if (outputFile != null) outputFile.close(); | 445 if (outputFile != null) outputFile.close(); |
452 } | 446 } |
453 } | 447 } |
454 } | 448 } |
OLD | NEW |