blob: 53fc8d4f6233a115c58950988d925a8b97d26ef1 [file] [log] [blame]
Fabien Sanglard0f29f542020-10-22 17:58:12 -07001/*
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
Jooyung Hanb72009a2021-07-13 13:40:12 +090023#include <getopt.h>
Fabien Sanglard0f29f542020-10-22 17:58:12 -070024#include <stdio.h>
25#include <stdlib.h>
26
27using namespace android;
28
29/*
30 * Show program usage.
31 */
32void 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");
Elliott Hughesf4800de2021-04-05 16:02:16 -070043 fprintf(stderr, " -p: page-align uncompressed .so files\n");
Fabien Sanglard0f29f542020-10-22 17:58:12 -070044 fprintf(stderr, " -v: verbose output\n");
45 fprintf(stderr, " -z: recompress using Zopfli\n");
46}
47
48
49/*
50 * Parse args.
51 */
52int 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
Jooyung Hanb72009a2021-07-13 13:40:12 +090064 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;
Fabien Sanglard0f29f542020-10-22 17:58:12 -070086 }
Fabien Sanglard0f29f542020-10-22 17:58:12 -070087 }
88
Jooyung Hanb72009a2021-07-13 13:40:12 +090089 if (!((check && (argc - optind) == 2) || (!check && (argc - optind) == 3))) {
Fabien Sanglard0f29f542020-10-22 17:58:12 -070090 wantUsage = true;
91 goto bail;
92 }
93
Jooyung Hanb72009a2021-07-13 13:40:12 +090094 alignment = strtol(argv[optind], &endp, 10);
Fabien Sanglard0f29f542020-10-22 17:58:12 -070095 if (*endp != '\0' || alignment <= 0) {
Jooyung Hanb72009a2021-07-13 13:40:12 +090096 fprintf(stderr, "Invalid value for alignment: %s\n", argv[optind]);
Fabien Sanglard0f29f542020-10-22 17:58:12 -070097 wantUsage = true;
98 goto bail;
99 }
100
101 if (check) {
102 /* check existing archive for correct alignment */
Jooyung Hanb72009a2021-07-13 13:40:12 +0900103 result = verify(argv[optind + 1], alignment, verbose, pageAlignSharedLibs);
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700104 } else {
105 /* create the new archive */
Jooyung Hanb72009a2021-07-13 13:40:12 +0900106 result = process(argv[optind + 1], argv[optind + 2], alignment, force, zopfli, pageAlignSharedLibs);
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700107
108 /* trust, but verify */
109 if (result == 0) {
Jooyung Hanb72009a2021-07-13 13:40:12 +0900110 result = verify(argv[optind + 2], alignment, verbose, pageAlignSharedLibs);
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700111 }
112 }
113
114bail:
115 if (wantUsage) {
116 usage();
117 result = 2;
118 }
119
120 return result;
121}