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] [-P <pagesize_kb>] [-v] [-z] <align> infile.zip outfile.zip\n"
38 " zipalign -c [-p] [-P <pagesize_kb>] [-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: 4kb page-align uncompressed .so files\n");
44 fprintf(stderr, " -v: verbose output\n");
45 fprintf(stderr, " -z: recompress using Zopfli\n");
46 fprintf(stderr, " -P <pagesize_kb>: Align uncompressed .so files to the specified\n");
47 fprintf(stderr, " page size. Valid values for <pagesize_kb> are 4, 16\n");
48 fprintf(stderr, " and 64. '-P' cannot be used in combination with '-p'.\n");
49 }
50
51
52 /*
53 * Parse args.
54 */
main(int argc,char * const argv[])55 int main(int argc, char* const argv[])
56 {
57 bool wantUsage = false;
58 bool check = false;
59 bool force = false;
60 bool verbose = false;
61 bool zopfli = false;
62 bool pageAlignSharedLibs = false;
63 int pageSize = 4096;
64 bool legacyPageAlignmentFlag = false; // -p
65 bool pageAlignmentFlag = false; // -P <pagesize_kb>
66 int result = 1;
67 int alignment;
68 char* endp;
69
70 int opt;
71
72 while ((opt = getopt(argc, argv, "fcpvzP:")) != -1) {
73 switch (opt) {
74 case 'c':
75 check = true;
76 break;
77 case 'f':
78 force = true;
79 break;
80 case 'v':
81 verbose = true;
82 break;
83 case 'z':
84 zopfli = true;
85 break;
86 case 'p':
87 legacyPageAlignmentFlag = true;
88 pageAlignSharedLibs = true;
89 pageSize = 4096;
90 break;
91 case 'P':
92 pageAlignmentFlag = true;
93 pageAlignSharedLibs = true;
94
95 if (!optarg) {
96 fprintf(stderr, "ERROR: -P requires an argument\n");
97 wantUsage = true;
98 goto bail;
99 }
100
101 pageSize = atoi(optarg);
102 if (pageSize != 4 && pageSize != 16 && pageSize != 64) {
103 fprintf(stderr, "ERROR: Invalid argument for -P: %s\n", optarg);
104 wantUsage = true;
105 goto bail;
106 }
107
108 pageSize *= 1024; // Convert from kB to bytes.
109
110 break;
111 default:
112 fprintf(stderr, "ERROR: unknown flag -%c\n", opt);
113 wantUsage = true;
114 goto bail;
115 }
116 }
117
118 if (legacyPageAlignmentFlag && pageAlignmentFlag) {
119 fprintf(stderr, "ERROR: Invalid options: '-P <pagesize_kb>' and '-p'"
120 "cannot be used in combination.\n");
121 wantUsage = true;
122 goto bail;
123 }
124
125 if (!((check && (argc - optind) == 2) || (!check && (argc - optind) == 3))) {
126 wantUsage = true;
127 goto bail;
128 }
129
130 alignment = strtol(argv[optind], &endp, 10);
131 if (*endp != '\0' || alignment <= 0) {
132 fprintf(stderr, "Invalid value for alignment: %s\n", argv[optind]);
133 wantUsage = true;
134 goto bail;
135 }
136
137 if (check) {
138 /* check existing archive for correct alignment */
139 result = verify(argv[optind + 1], alignment, verbose, pageAlignSharedLibs, pageSize);
140 } else {
141 /* create the new archive */
142 result = process(argv[optind + 1], argv[optind + 2], alignment, force, zopfli,
143 pageAlignSharedLibs, pageSize);
144
145 /* trust, but verify */
146 if (result == 0) {
147 result = verify(argv[optind + 2], alignment, verbose, pageAlignSharedLibs, pageSize);
148 }
149 }
150
151 bail:
152 if (wantUsage) {
153 usage();
154 result = 2;
155 }
156
157 return result;
158 }
159