blob: 23840e39456b4733f2b2b5ec5adb1fef546558ed [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
Mathias Agopian3344b2e2009-06-05 14:55:48 -070017#include "ZipFile.h"
The Android Open Source Project88b60792009-03-03 19:28:42 -080018
The Android Open Source Project88b60792009-03-03 19:28:42 -080019#include <stdio.h>
Mark Salyzyn404fd5b2016-10-17 10:20:33 -070020#include <stdlib.h>
21#include <unistd.h>
The Android Open Source Project88b60792009-03-03 19:28:42 -080022
Fabien Sanglard0f29f542020-10-22 17:58:12 -070023namespace android {
The Android Open Source Project88b60792009-03-03 19:28:42 -080024
Fabien Sanglard8163cfa2022-10-10 22:19:47 +000025// An entry is considered a directory if it has a stored size of zero
26// and it ends with '/' or '\' character.
27static 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
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070038static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
39 ZipEntry* pEntry) {
40
Narayan Kamathe0b8d192015-02-26 17:57:55 +000041 static const int kPageAlignment = 4096;
42
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070043 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
The Android Open Source Project88b60792009-03-03 19:28:42 -080055/*
56 * Copy all entries from "pZin" to "pZout", aligning as needed.
57 */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070058static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
Dan Willemsenb589ae42015-10-29 21:26:18 +000059 bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -080060{
61 int numEntries = pZin->getNumEntries();
62 ZipEntry* pEntry;
The Android Open Source Project88b60792009-03-03 19:28:42 -080063 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
Fabien Sanglard8163cfa2022-10-10 22:19:47 +000075 if (pEntry->isCompressed() || isDirectory(pEntry)) {
The Android Open Source Project88b60792009-03-03 19:28:42 -080076 /* 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
Raph Levien093d04c2014-07-07 16:00:29 -070081 if (zopfli) {
Dan Willemsenb589ae42015-10-29 21:26:18 +000082 status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
Raph Levien093d04c2014-07-07 16:00:29 -070083 } else {
Dan Willemsenb589ae42015-10-29 21:26:18 +000084 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
Raph Levien093d04c2014-07-07 16:00:29 -070085 }
The Android Open Source Project88b60792009-03-03 19:28:42 -080086 } else {
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070087 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
88
The Android Open Source Project88b60792009-03-03 19:28:42 -080089 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
90 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
91 // bias, (long) pEntry->getUncompressedLen(), padding);
Fabien Sanglarda7206352020-10-20 15:47:10 -070092 status = pZout->add(pZin, pEntry, alignTo, &pNewEntry);
The Android Open Source Project88b60792009-03-03 19:28:42 -080093 }
94
Elliott Hughesad7d5622018-10-08 11:19:28 -070095 if (status != OK)
The Android Open Source Project88b60792009-03-03 19:28:42 -080096 return 1;
The Android Open Source Project88b60792009-03-03 19:28:42 -080097 //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 */
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700109int process(const char* inFileName, const char* outFileName,
Dan Willemsenb589ae42015-10-29 21:26:18 +0000110 int alignment, bool force, bool zopfli, bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800111{
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
Elliott Hughesad7d5622018-10-08 11:19:28 -0700129 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800130 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)
Elliott Hughesad7d5622018-10-08 11:19:28 -0700135 != OK)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800136 {
Jingwen Owen Ou30321392012-08-17 16:21:11 -0700137 fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800138 return 1;
139 }
140
Dan Willemsenb589ae42015-10-29 21:26:18 +0000141 int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800142 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 */
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700152int verify(const char* fileName, int alignment, bool verbose,
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700153 bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800154{
155 ZipFile zipFile;
156 bool foundBad = false;
157
158 if (verbose)
159 printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
160
Elliott Hughesad7d5622018-10-08 11:19:28 -0700161 if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != OK) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800162 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) {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700173 printf("%8jd %s (OK - compressed)\n",
174 (intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800175 }
Fabien Sanglard8163cfa2022-10-10 22:19:47 +0000176 } 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 {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700183 off_t offset = pEntry->getFileOffset();
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700184 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
185 if ((offset % alignTo) != 0) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800186 if (verbose) {
Chih-Hung Hsieh9b2bf2d2018-08-11 11:33:43 -0700187 printf("%8jd %s (BAD - %jd)\n",
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700188 (intmax_t) offset, pEntry->getFileName(),
Chih-Hung Hsieh9b2bf2d2018-08-11 11:33:43 -0700189 (intmax_t) (offset % alignTo));
The Android Open Source Project88b60792009-03-03 19:28:42 -0800190 }
191 foundBad = true;
192 } else {
193 if (verbose) {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700194 printf("%8jd %s (OK)\n",
195 (intmax_t) offset, pEntry->getFileName());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800196 }
197 }
198 }
199 }
200
201 if (verbose)
202 printf("Verification %s\n", foundBad ? "FAILED" : "succesful");
203
204 return foundBad ? 1 : 0;
205}
206
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700207} // namespace android