• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
24 #include <stdlib.h>
25 
26 using namespace android;
27 
28 /*
29  * Show program usage.
30  */
usage(void)31 void usage(void)
32 {
33     fprintf(stderr, "Zip alignment utility\n");
34     fprintf(stderr, "Copyright (C) 2009 The Android Open Source Project\n\n");
35     fprintf(stderr,
36         "Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip\n"
37         "       zipalign -c [-p] [-v] <align> infile.zip\n\n" );
38     fprintf(stderr,
39         "  <align>: alignment in bytes, e.g. '4' provides 32-bit alignment\n");
40     fprintf(stderr, "  -c: check alignment only (does not modify file)\n");
41     fprintf(stderr, "  -f: overwrite existing outfile.zip\n");
42     fprintf(stderr, "  -p: page-align uncompressed .so files\n");
43     fprintf(stderr, "  -v: verbose output\n");
44     fprintf(stderr, "  -z: recompress using Zopfli\n");
45 }
46 
47 
48 /*
49  * Parse args.
50  */
main(int argc,char * const argv[])51 int main(int argc, char* const argv[])
52 {
53     bool wantUsage = false;
54     bool check = false;
55     bool force = false;
56     bool verbose = false;
57     bool zopfli = false;
58     bool pageAlignSharedLibs = false;
59     int result = 1;
60     int alignment;
61     char* endp;
62 
63     if (argc < 4) {
64         wantUsage = true;
65         goto bail;
66     }
67 
68     argc--;
69     argv++;
70 
71     while (argc && argv[0][0] == '-') {
72         const char* cp = argv[0] +1;
73 
74         while (*cp != '\0') {
75             switch (*cp) {
76             case 'c':
77                 check = true;
78                 break;
79             case 'f':
80                 force = true;
81                 break;
82             case 'v':
83                 verbose = true;
84                 break;
85             case 'z':
86                 zopfli = true;
87                 break;
88             case 'p':
89                 pageAlignSharedLibs = true;
90                 break;
91             default:
92                 fprintf(stderr, "ERROR: unknown flag -%c\n", *cp);
93                 wantUsage = true;
94                 goto bail;
95             }
96 
97             cp++;
98         }
99 
100         argc--;
101         argv++;
102     }
103 
104     if (!((check && argc == 2) || (!check && argc == 3))) {
105         wantUsage = true;
106         goto bail;
107     }
108 
109     alignment = strtol(argv[0], &endp, 10);
110     if (*endp != '\0' || alignment <= 0) {
111         fprintf(stderr, "Invalid value for alignment: %s\n", argv[0]);
112         wantUsage = true;
113         goto bail;
114     }
115 
116     if (check) {
117         /* check existing archive for correct alignment */
118         result = verify(argv[1], alignment, verbose, pageAlignSharedLibs);
119     } else {
120         /* create the new archive */
121         result = process(argv[1], argv[2], alignment, force, zopfli, pageAlignSharedLibs);
122 
123         /* trust, but verify */
124         if (result == 0) {
125             result = verify(argv[2], alignment, verbose, pageAlignSharedLibs);
126         }
127     }
128 
129 bail:
130     if (wantUsage) {
131         usage();
132         result = 2;
133     }
134 
135     return result;
136 }
137