blob: a2dfd02891144b5747a30f6e99e991b119617f80 [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
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 */
Andy McFadden95ed76b2009-09-29 10:55:32 -070016
The Android Open Source Project88b60792009-03-03 19:28:42 -080017/*
18 * Zip alignment tool
19 */
Mathias Agopian3344b2e2009-06-05 14:55:48 -070020#include "ZipFile.h"
The Android Open Source Project88b60792009-03-03 19:28:42 -080021
22#include <stdlib.h>
23#include <stdio.h>
24
25using namespace android;
26
27/*
28 * Show program usage.
29 */
30void usage(void)
31{
32 fprintf(stderr, "Zip alignment utility\n");
Andy McFadden95ed76b2009-09-29 10:55:32 -070033 fprintf(stderr, "Copyright (C) 2009 The Android Open Source Project\n\n");
The Android Open Source Project88b60792009-03-03 19:28:42 -080034 fprintf(stderr,
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070035 "Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip\n"
Narayan Kamathe0b8d192015-02-26 17:57:55 +000036 " zipalign -c [-v] <align> infile.zip\n\n" );
Andy McFadden95ed76b2009-09-29 10:55:32 -070037 fprintf(stderr,
38 " <align>: alignment in bytes, e.g. '4' provides 32-bit alignment\n");
39 fprintf(stderr, " -c: check alignment only (does not modify file)\n");
40 fprintf(stderr, " -f: overwrite existing outfile.zip\n");
Narayan Kamathe0b8d192015-02-26 17:57:55 +000041 fprintf(stderr, " -p: page align stored shared object files\n");
Andy McFadden95ed76b2009-09-29 10:55:32 -070042 fprintf(stderr, " -v: verbose output\n");
Raph Levien093d04c2014-07-07 16:00:29 -070043 fprintf(stderr, " -z: recompress using Zopfli\n");
The Android Open Source Project88b60792009-03-03 19:28:42 -080044}
45
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070046static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
47 ZipEntry* pEntry) {
48
Narayan Kamathe0b8d192015-02-26 17:57:55 +000049 static const int kPageAlignment = 4096;
50
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070051 if (!pageAlignSharedLibs) {
52 return defaultAlignment;
53 }
54
55 const char* ext = strrchr(pEntry->getFileName(), '.');
56 if (ext && strcmp(ext, ".so") == 0) {
57 return kPageAlignment;
58 }
59
60 return defaultAlignment;
61}
62
The Android Open Source Project88b60792009-03-03 19:28:42 -080063/*
64 * Copy all entries from "pZin" to "pZout", aligning as needed.
65 */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070066static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
67 bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -080068{
69 int numEntries = pZin->getNumEntries();
70 ZipEntry* pEntry;
71 int bias = 0;
72 status_t status;
73
74 for (int i = 0; i < numEntries; i++) {
75 ZipEntry* pNewEntry;
76 int padding = 0;
77
78 pEntry = pZin->getEntryByIndex(i);
79 if (pEntry == NULL) {
80 fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i);
81 return 1;
82 }
83
84 if (pEntry->isCompressed()) {
85 /* copy the entry without padding */
86 //printf("--- %s: orig at %ld len=%ld (compressed)\n",
87 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
88 // (long) pEntry->getUncompressedLen());
89
Raph Levien093d04c2014-07-07 16:00:29 -070090 if (zopfli) {
91 status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
92 bias += pNewEntry->getCompressedLen() - pEntry->getCompressedLen();
93 } else {
94 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
95 }
The Android Open Source Project88b60792009-03-03 19:28:42 -080096 } else {
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070097 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
98
The Android Open Source Project88b60792009-03-03 19:28:42 -080099 /*
100 * Copy the entry, adjusting as required. We assume that the
101 * file position in the new file will be equal to the file
102 * position in the original.
103 */
104 long newOffset = pEntry->getFileOffset() + bias;
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700105 padding = (alignTo - (newOffset % alignTo)) % alignTo;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800106
107 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
108 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
109 // bias, (long) pEntry->getUncompressedLen(), padding);
Raph Levien093d04c2014-07-07 16:00:29 -0700110 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800111 }
112
The Android Open Source Project88b60792009-03-03 19:28:42 -0800113 if (status != NO_ERROR)
114 return 1;
115 bias += padding;
116 //printf(" added '%s' at %ld (pad=%d)\n",
117 // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(),
118 // padding);
119 }
120
121 return 0;
122}
123
124/*
125 * Process a file. We open the input and output files, failing if the
126 * output file exists and "force" wasn't specified.
127 */
128static int process(const char* inFileName, const char* outFileName,
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700129 int alignment, bool force, bool zopfli, bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800130{
131 ZipFile zin, zout;
132
133 //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n",
134 // alignment, inFileName, outFileName, force);
135
136 /* this mode isn't supported -- do a trivial check */
137 if (strcmp(inFileName, outFileName) == 0) {
138 fprintf(stderr, "Input and output can't be same file\n");
139 return 1;
140 }
141
142 /* don't overwrite existing unless given permission */
143 if (!force && access(outFileName, F_OK) == 0) {
144 fprintf(stderr, "Output file '%s' exists\n", outFileName);
145 return 1;
146 }
147
148 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != NO_ERROR) {
149 fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName);
150 return 1;
151 }
152 if (zout.open(outFileName,
153 ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate)
154 != NO_ERROR)
155 {
Jingwen Owen Ou30321392012-08-17 16:21:11 -0700156 fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800157 return 1;
158 }
159
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700160 int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800161 if (result != 0) {
162 printf("zipalign: failed rewriting '%s' to '%s'\n",
163 inFileName, outFileName);
164 }
165 return result;
166}
167
168/*
169 * Verify the alignment of a zip archive.
170 */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700171static int verify(const char* fileName, int alignment, bool verbose,
172 bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800173{
174 ZipFile zipFile;
175 bool foundBad = false;
176
177 if (verbose)
178 printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
179
180 if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != NO_ERROR) {
181 fprintf(stderr, "Unable to open '%s' for verification\n", fileName);
182 return 1;
183 }
184
185 int numEntries = zipFile.getNumEntries();
186 ZipEntry* pEntry;
187
188 for (int i = 0; i < numEntries; i++) {
189 pEntry = zipFile.getEntryByIndex(i);
190 if (pEntry->isCompressed()) {
191 if (verbose) {
Doug Zongker3f2933b2009-04-16 10:16:38 -0700192 printf("%8ld %s (OK - compressed)\n",
The Android Open Source Project88b60792009-03-03 19:28:42 -0800193 (long) pEntry->getFileOffset(), pEntry->getFileName());
194 }
195 } else {
196 long offset = pEntry->getFileOffset();
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700197 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
198 if ((offset % alignTo) != 0) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800199 if (verbose) {
Doug Zongker3f2933b2009-04-16 10:16:38 -0700200 printf("%8ld %s (BAD - %ld)\n",
The Android Open Source Project88b60792009-03-03 19:28:42 -0800201 (long) offset, pEntry->getFileName(),
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700202 offset % alignTo);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800203 }
204 foundBad = true;
205 } else {
206 if (verbose) {
207 printf("%8ld %s (OK)\n",
208 (long) offset, pEntry->getFileName());
209 }
210 }
211 }
212 }
213
214 if (verbose)
215 printf("Verification %s\n", foundBad ? "FAILED" : "succesful");
216
217 return foundBad ? 1 : 0;
218}
219
220/*
221 * Parse args.
222 */
223int main(int argc, char* const argv[])
224{
225 bool wantUsage = false;
Doug Zongker3f2933b2009-04-16 10:16:38 -0700226 bool check = false;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800227 bool force = false;
228 bool verbose = false;
Raph Levien093d04c2014-07-07 16:00:29 -0700229 bool zopfli = false;
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700230 bool pageAlignSharedLibs = false;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800231 int result = 1;
232 int alignment;
233 char* endp;
234
235 if (argc < 4) {
236 wantUsage = true;
237 goto bail;
238 }
239
240 argc--;
241 argv++;
242
243 while (argc && argv[0][0] == '-') {
244 const char* cp = argv[0] +1;
245
246 while (*cp != '\0') {
247 switch (*cp) {
Doug Zongker3f2933b2009-04-16 10:16:38 -0700248 case 'c':
249 check = true;
250 break;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800251 case 'f':
252 force = true;
253 break;
254 case 'v':
255 verbose = true;
256 break;
Raph Levien093d04c2014-07-07 16:00:29 -0700257 case 'z':
258 zopfli = true;
259 break;
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700260 case 'p':
261 pageAlignSharedLibs = true;
262 break;
The Android Open Source Project88b60792009-03-03 19:28:42 -0800263 default:
264 fprintf(stderr, "ERROR: unknown flag -%c\n", *cp);
265 wantUsage = true;
266 goto bail;
267 }
268
269 cp++;
270 }
271
272 argc--;
273 argv++;
274 }
275
Doug Zongker3f2933b2009-04-16 10:16:38 -0700276 if (!((check && argc == 2) || (!check && argc == 3))) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800277 wantUsage = true;
278 goto bail;
279 }
280
281 alignment = strtol(argv[0], &endp, 10);
282 if (*endp != '\0' || alignment <= 0) {
283 fprintf(stderr, "Invalid value for alignment: %s\n", argv[0]);
284 wantUsage = true;
285 goto bail;
286 }
287
Doug Zongker3f2933b2009-04-16 10:16:38 -0700288 if (check) {
289 /* check existing archive for correct alignment */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700290 result = verify(argv[1], alignment, verbose, pageAlignSharedLibs);
Doug Zongker3f2933b2009-04-16 10:16:38 -0700291 } else {
292 /* create the new archive */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700293 result = process(argv[1], argv[2], alignment, force, zopfli, pageAlignSharedLibs);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800294
Doug Zongker3f2933b2009-04-16 10:16:38 -0700295 /* trust, but verify */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700296 if (result == 0) {
297 result = verify(argv[2], alignment, verbose, pageAlignSharedLibs);
298 }
Doug Zongker3f2933b2009-04-16 10:16:38 -0700299 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800300
301bail:
302 if (wantUsage) {
303 usage();
304 result = 2;
305 }
306
307 return result;
308}