blob: 3d65bc0fc2310966898c95f3eabc7f05e96472db [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>
Kalesh Singh55405b62023-08-17 09:44:45 -070020#include <string.h>
Mark Salyzyn404fd5b2016-10-17 10:20:33 -070021#include <stdlib.h>
22#include <unistd.h>
The Android Open Source Project88b60792009-03-03 19:28:42 -080023
Fabien Sanglard0f29f542020-10-22 17:58:12 -070024namespace android {
The Android Open Source Project88b60792009-03-03 19:28:42 -080025
Fabien Sanglard8163cfa2022-10-10 22:19:47 +000026// An entry is considered a directory if it has a stored size of zero
27// and it ends with '/' or '\' character.
28static bool isDirectory(ZipEntry* entry) {
29 if (entry->getUncompressedLen() != 0) {
30 return false;
31 }
32
33 const char* name = entry->getFileName();
34 size_t nameLength = strlen(name);
35 char lastChar = name[nameLength-1];
36 return lastChar == '/' || lastChar == '\\';
37}
38
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070039static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
Kalesh Singh55405b62023-08-17 09:44:45 -070040 ZipEntry* pEntry, int pageSize) {
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070041 if (!pageAlignSharedLibs) {
42 return defaultAlignment;
43 }
44
45 const char* ext = strrchr(pEntry->getFileName(), '.');
46 if (ext && strcmp(ext, ".so") == 0) {
Kalesh Singh55405b62023-08-17 09:44:45 -070047 return pageSize;
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070048 }
49
50 return defaultAlignment;
51}
52
The Android Open Source Project88b60792009-03-03 19:28:42 -080053/*
54 * Copy all entries from "pZin" to "pZout", aligning as needed.
55 */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070056static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
Kalesh Singh55405b62023-08-17 09:44:45 -070057 bool pageAlignSharedLibs, int pageSize)
The Android Open Source Project88b60792009-03-03 19:28:42 -080058{
59 int numEntries = pZin->getNumEntries();
60 ZipEntry* pEntry;
The Android Open Source Project88b60792009-03-03 19:28:42 -080061 status_t status;
62
63 for (int i = 0; i < numEntries; i++) {
64 ZipEntry* pNewEntry;
65 int padding = 0;
66
67 pEntry = pZin->getEntryByIndex(i);
68 if (pEntry == NULL) {
69 fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i);
70 return 1;
71 }
72
Fabien Sanglard8163cfa2022-10-10 22:19:47 +000073 if (pEntry->isCompressed() || isDirectory(pEntry)) {
The Android Open Source Project88b60792009-03-03 19:28:42 -080074 /* copy the entry without padding */
75 //printf("--- %s: orig at %ld len=%ld (compressed)\n",
76 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
77 // (long) pEntry->getUncompressedLen());
78
Raph Levien093d04c2014-07-07 16:00:29 -070079 if (zopfli) {
Dan Willemsenb589ae42015-10-29 21:26:18 +000080 status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
Raph Levien093d04c2014-07-07 16:00:29 -070081 } else {
Dan Willemsenb589ae42015-10-29 21:26:18 +000082 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
Raph Levien093d04c2014-07-07 16:00:29 -070083 }
The Android Open Source Project88b60792009-03-03 19:28:42 -080084 } else {
Kalesh Singh55405b62023-08-17 09:44:45 -070085 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry,
86 pageSize);
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070087
The Android Open Source Project88b60792009-03-03 19:28:42 -080088 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
89 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
90 // bias, (long) pEntry->getUncompressedLen(), padding);
Fabien Sanglarda7206352020-10-20 15:47:10 -070091 status = pZout->add(pZin, pEntry, alignTo, &pNewEntry);
The Android Open Source Project88b60792009-03-03 19:28:42 -080092 }
93
Elliott Hughesad7d5622018-10-08 11:19:28 -070094 if (status != OK)
The Android Open Source Project88b60792009-03-03 19:28:42 -080095 return 1;
The Android Open Source Project88b60792009-03-03 19:28:42 -080096 //printf(" added '%s' at %ld (pad=%d)\n",
97 // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(),
98 // padding);
99 }
100
101 return 0;
102}
103
104/*
105 * Process a file. We open the input and output files, failing if the
106 * output file exists and "force" wasn't specified.
107 */
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700108int process(const char* inFileName, const char* outFileName,
Kalesh Singh55405b62023-08-17 09:44:45 -0700109 int alignment, bool force, bool zopfli, bool pageAlignSharedLibs, int pageSize)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800110{
111 ZipFile zin, zout;
112
113 //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n",
114 // alignment, inFileName, outFileName, force);
115
116 /* this mode isn't supported -- do a trivial check */
117 if (strcmp(inFileName, outFileName) == 0) {
118 fprintf(stderr, "Input and output can't be same file\n");
119 return 1;
120 }
121
122 /* don't overwrite existing unless given permission */
123 if (!force && access(outFileName, F_OK) == 0) {
124 fprintf(stderr, "Output file '%s' exists\n", outFileName);
125 return 1;
126 }
127
Elliott Hughesad7d5622018-10-08 11:19:28 -0700128 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) {
Kalesh Singh55405b62023-08-17 09:44:45 -0700129 fprintf(stderr, "Unable to open '%s' as zip archive: %s\n", inFileName, strerror(errno));
The Android Open Source Project88b60792009-03-03 19:28:42 -0800130 return 1;
131 }
132 if (zout.open(outFileName,
133 ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate)
Elliott Hughesad7d5622018-10-08 11:19:28 -0700134 != OK)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800135 {
Jingwen Owen Ou30321392012-08-17 16:21:11 -0700136 fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800137 return 1;
138 }
139
Kalesh Singh55405b62023-08-17 09:44:45 -0700140 int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs,
141 pageSize);
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,
Kalesh Singh55405b62023-08-17 09:44:45 -0700153 bool pageAlignSharedLibs, int pageSize)
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();
Kalesh Singh55405b62023-08-17 09:44:45 -0700184 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry,
185 pageSize);
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700186 if ((offset % alignTo) != 0) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800187 if (verbose) {
Chih-Hung Hsieh9b2bf2d2018-08-11 11:33:43 -0700188 printf("%8jd %s (BAD - %jd)\n",
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700189 (intmax_t) offset, pEntry->getFileName(),
Chih-Hung Hsieh9b2bf2d2018-08-11 11:33:43 -0700190 (intmax_t) (offset % alignTo));
The Android Open Source Project88b60792009-03-03 19:28:42 -0800191 }
192 foundBad = true;
193 } else {
194 if (verbose) {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700195 printf("%8jd %s (OK)\n",
196 (intmax_t) offset, pEntry->getFileName());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800197 }
198 }
199 }
200 }
201
202 if (verbose)
Steven Morelandcffced32024-06-11 17:07:44 +0000203 printf("Verification %s\n", foundBad ? "FAILED" : "successful");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800204
205 return foundBad ? 1 : 0;
206}
207
Fabien Sanglard0f29f542020-10-22 17:58:12 -0700208} // namespace android