1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 * Zip alignment tool
19 */
20
21 #include "ZipAlign.h"
22
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 using namespace android;
28
29 /*
30 * Show program usage.
31 */
usage(void)32 void usage(void)
33 {
34 fprintf(stderr, "Zip alignment utility\n");
35 fprintf(stderr, "Copyright (C) 2009 The Android Open Source Project\n\n");
36 fprintf(stderr,
37 "Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip\n"
38 " zipalign -c [-p] [-v] <align> infile.zip\n\n" );
39 fprintf(stderr,
40 " <align>: alignment in bytes, e.g. '4' provides 32-bit alignment\n");
41 fprintf(stderr, " -c: check alignment only (does not modify file)\n");
42 fprintf(stderr, " -f: overwrite existing outfile.zip\n");
43 fprintf(stderr, " -p: page-align uncompressed .so files\n");
44 fprintf(stderr, " -v: verbose output\n");
45 fprintf(stderr, " -z: recompress using Zopfli\n");
46 }
47
48
49 /*
50 * Parse args.
51 */
main(int argc,char * const argv[])52 int main(int argc, char* const argv[])
53 {
54 bool wantUsage = false;
55 bool check = false;
56 bool force = false;
57 bool verbose = false;
58 bool zopfli = false;
59 bool pageAlignSharedLibs = false;
60 int result = 1;
61 int alignment;
62 char* endp;
63
64 int opt;
65 while ((opt = getopt(argc, argv, "fcpvz")) != -1) {
66 switch (opt) {
67 case 'c':
68 check = true;
69 break;
70 case 'f':
71 force = true;
72 break;
73 case 'v':
74 verbose = true;
75 break;
76 case 'z':
77 zopfli = true;
78 break;
79 case 'p':
80 pageAlignSharedLibs = true;
81 break;
82 default:
83 fprintf(stderr, "ERROR: unknown flag -%c\n", opt);
84 wantUsage = true;
85 goto bail;
86 }
87 }
88
89 if (!((check && (argc - optind) == 2) || (!check && (argc - optind) == 3))) {
90 wantUsage = true;
91 goto bail;
92 }
93
94 alignment = strtol(argv[optind], &endp, 10);
95 if (*endp != '\0' || alignment <= 0) {
96 fprintf(stderr, "Invalid value for alignment: %s\n", argv[optind]);
97 wantUsage = true;
98 goto bail;
99 }
100
101 if (check) {
102 /* check existing archive for correct alignment */
103 result = verify(argv[optind + 1], alignment, verbose, pageAlignSharedLibs);
104 } else {
105 /* create the new archive */
106 result = process(argv[optind + 1], argv[optind + 2], alignment, force, zopfli, pageAlignSharedLibs);
107
108 /* trust, but verify */
109 if (result == 0) {
110 result = verify(argv[optind + 2], alignment, verbose, pageAlignSharedLibs);
111 }
112 }
113
114 bail:
115 if (wantUsage) {
116 usage();
117 result = 2;
118 }
119
120 return result;
121 }
122