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 #include "ZipFile.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 namespace android {
24
25 // An entry is considered a directory if it has a stored size of zero
26 // and it ends with '/' or '\' character.
isDirectory(ZipEntry * entry)27 static bool isDirectory(ZipEntry* entry) {
28 if (entry->getUncompressedLen() != 0) {
29 return false;
30 }
31
32 const char* name = entry->getFileName();
33 size_t nameLength = strlen(name);
34 char lastChar = name[nameLength-1];
35 return lastChar == '/' || lastChar == '\\';
36 }
37
getAlignment(bool pageAlignSharedLibs,int defaultAlignment,ZipEntry * pEntry)38 static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
39 ZipEntry* pEntry) {
40
41 static const int kPageAlignment = 4096;
42
43 if (!pageAlignSharedLibs) {
44 return defaultAlignment;
45 }
46
47 const char* ext = strrchr(pEntry->getFileName(), '.');
48 if (ext && strcmp(ext, ".so") == 0) {
49 return kPageAlignment;
50 }
51
52 return defaultAlignment;
53 }
54
55 /*
56 * Copy all entries from "pZin" to "pZout", aligning as needed.
57 */
copyAndAlign(ZipFile * pZin,ZipFile * pZout,int alignment,bool zopfli,bool pageAlignSharedLibs)58 static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
59 bool pageAlignSharedLibs)
60 {
61 int numEntries = pZin->getNumEntries();
62 ZipEntry* pEntry;
63 status_t status;
64
65 for (int i = 0; i < numEntries; i++) {
66 ZipEntry* pNewEntry;
67 int padding = 0;
68
69 pEntry = pZin->getEntryByIndex(i);
70 if (pEntry == NULL) {
71 fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i);
72 return 1;
73 }
74
75 if (pEntry->isCompressed() || isDirectory(pEntry)) {
76 /* copy the entry without padding */
77 //printf("--- %s: orig at %ld len=%ld (compressed)\n",
78 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
79 // (long) pEntry->getUncompressedLen());
80
81 if (zopfli) {
82 status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
83 } else {
84 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
85 }
86 } else {
87 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
88
89 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
90 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
91 // bias, (long) pEntry->getUncompressedLen(), padding);
92 status = pZout->add(pZin, pEntry, alignTo, &pNewEntry);
93 }
94
95 if (status != OK)
96 return 1;
97 //printf(" added '%s' at %ld (pad=%d)\n",
98 // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(),
99 // padding);
100 }
101
102 return 0;
103 }
104
105 /*
106 * Process a file. We open the input and output files, failing if the
107 * output file exists and "force" wasn't specified.
108 */
process(const char * inFileName,const char * outFileName,int alignment,bool force,bool zopfli,bool pageAlignSharedLibs)109 int process(const char* inFileName, const char* outFileName,
110 int alignment, bool force, bool zopfli, bool pageAlignSharedLibs)
111 {
112 ZipFile zin, zout;
113
114 //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n",
115 // alignment, inFileName, outFileName, force);
116
117 /* this mode isn't supported -- do a trivial check */
118 if (strcmp(inFileName, outFileName) == 0) {
119 fprintf(stderr, "Input and output can't be same file\n");
120 return 1;
121 }
122
123 /* don't overwrite existing unless given permission */
124 if (!force && access(outFileName, F_OK) == 0) {
125 fprintf(stderr, "Output file '%s' exists\n", outFileName);
126 return 1;
127 }
128
129 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) {
130 fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName);
131 return 1;
132 }
133 if (zout.open(outFileName,
134 ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate)
135 != OK)
136 {
137 fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
138 return 1;
139 }
140
141 int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs);
142 if (result != 0) {
143 printf("zipalign: failed rewriting '%s' to '%s'\n",
144 inFileName, outFileName);
145 }
146 return result;
147 }
148
149 /*
150 * Verify the alignment of a zip archive.
151 */
verify(const char * fileName,int alignment,bool verbose,bool pageAlignSharedLibs)152 int verify(const char* fileName, int alignment, bool verbose,
153 bool pageAlignSharedLibs)
154 {
155 ZipFile zipFile;
156 bool foundBad = false;
157
158 if (verbose)
159 printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
160
161 if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != OK) {
162 fprintf(stderr, "Unable to open '%s' for verification\n", fileName);
163 return 1;
164 }
165
166 int numEntries = zipFile.getNumEntries();
167 ZipEntry* pEntry;
168
169 for (int i = 0; i < numEntries; i++) {
170 pEntry = zipFile.getEntryByIndex(i);
171 if (pEntry->isCompressed()) {
172 if (verbose) {
173 printf("%8jd %s (OK - compressed)\n",
174 (intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
175 }
176 } else if(isDirectory(pEntry)) {
177 // Directory entries do not need to be aligned.
178 if (verbose)
179 printf("%8jd %s (OK - directory)\n",
180 (intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
181 continue;
182 } else {
183 off_t offset = pEntry->getFileOffset();
184 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
185 if ((offset % alignTo) != 0) {
186 if (verbose) {
187 printf("%8jd %s (BAD - %jd)\n",
188 (intmax_t) offset, pEntry->getFileName(),
189 (intmax_t) (offset % alignTo));
190 }
191 foundBad = true;
192 } else {
193 if (verbose) {
194 printf("%8jd %s (OK)\n",
195 (intmax_t) offset, pEntry->getFileName());
196 }
197 }
198 }
199 }
200
201 if (verbose)
202 printf("Verification %s\n", foundBad ? "FAILED" : "succesful");
203
204 return foundBad ? 1 : 0;
205 }
206
207 } // namespace android
208