blob: 2f24403a1a1870211b72f11ccd5ab53a4607f1d5 [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,
Kalesh Singh55405b62023-08-17 09:44:45 -070037 "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" );
Fabien Sanglard0f29f542020-10-22 17:58:12 -070039 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");
Kalesh Singh55405b62023-08-17 09:44:45 -070043 fprintf(stderr, " -p: 4kb 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");
Kalesh Singh55405b62023-08-17 09:44:45 -070046 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");
Fabien Sanglard0f29f542020-10-22 17:58:12 -070049}
50
51
52/*
53 * Parse args.
54 */
55int 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;
Kalesh Singh55405b62023-08-17 09:44:45 -070063 int pageSize = 4096;
64 bool legacyPageAlignmentFlag = false; // -p
65 bool pageAlignmentFlag = false; // -P <pagesize_kb>
Fabien Sanglard0f29f542020-10-22 17:58:12 -070066 int result = 1;
67 int alignment;
68 char* endp;
69
Jooyung Hanb72009a2021-07-13 13:40:12 +090070 int opt;
Kalesh Singh55405b62023-08-17 09:44:45 -070071
72 while ((opt = getopt(argc, argv, "fcpvzP:")) != -1) {
Jooyung Hanb72009a2021-07-13 13:40:12 +090073 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':
Kalesh Singh55405b62023-08-17 09:44:45 -070087 legacyPageAlignmentFlag = true;
Jooyung Hanb72009a2021-07-13 13:40:12 +090088 pageAlignSharedLibs = true;
Kalesh Singh55405b62023-08-17 09:44:45 -070089 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
Jooyung Hanb72009a2021-07-13 13:40:12 +0900110 break;
111 default:
112 fprintf(stderr, "ERROR: unknown flag -%c\n", opt);
113 wantUsage = true;
114 goto bail;
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700115 }
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700116 }
117
Kalesh Singh55405b62023-08-17 09:44:45 -0700118 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
Jooyung Hanb72009a2021-07-13 13:40:12 +0900125 if (!((check && (argc - optind) == 2) || (!check && (argc - optind) == 3))) {
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700126 wantUsage = true;
127 goto bail;
128 }
129
Jooyung Hanb72009a2021-07-13 13:40:12 +0900130 alignment = strtol(argv[optind], &endp, 10);
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700131 if (*endp != '\0' || alignment <= 0) {
Jooyung Hanb72009a2021-07-13 13:40:12 +0900132 fprintf(stderr, "Invalid value for alignment: %s\n", argv[optind]);
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700133 wantUsage = true;
134 goto bail;
135 }
136
137 if (check) {
138 /* check existing archive for correct alignment */
Kalesh Singh55405b62023-08-17 09:44:45 -0700139 result = verify(argv[optind + 1], alignment, verbose, pageAlignSharedLibs, pageSize);
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700140 } else {
141 /* create the new archive */
Kalesh Singh55405b62023-08-17 09:44:45 -0700142 result = process(argv[optind + 1], argv[optind + 2], alignment, force, zopfli,
143 pageAlignSharedLibs, pageSize);
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700144
145 /* trust, but verify */
146 if (result == 0) {
Kalesh Singh55405b62023-08-17 09:44:45 -0700147 result = verify(argv[optind + 2], alignment, verbose, pageAlignSharedLibs, pageSize);
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700148 }
149 }
150
151bail:
152 if (wantUsage) {
153 usage();
154 result = 2;
155 }
156
157 return result;
158}