blob: 1851ac562cce8e1cc256f01fc80e70a3add3878c [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 Sanglardd4f71a92020-10-22 17:58:12 -070023namespace android {
The Android Open Source Project88b60792009-03-03 19:28:42 -080024
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070025static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
26 ZipEntry* pEntry) {
27
Narayan Kamathe0b8d192015-02-26 17:57:55 +000028 static const int kPageAlignment = 4096;
29
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070030 if (!pageAlignSharedLibs) {
31 return defaultAlignment;
32 }
33
34 const char* ext = strrchr(pEntry->getFileName(), '.');
35 if (ext && strcmp(ext, ".so") == 0) {
36 return kPageAlignment;
37 }
38
39 return defaultAlignment;
40}
41
The Android Open Source Project88b60792009-03-03 19:28:42 -080042/*
43 * Copy all entries from "pZin" to "pZout", aligning as needed.
44 */
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070045static int copyAndAlign(ZipFile* pZin, ZipFile* pZout, int alignment, bool zopfli,
Dan Willemsenb589ae42015-10-29 21:26:18 +000046 bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -080047{
48 int numEntries = pZin->getNumEntries();
49 ZipEntry* pEntry;
50 int bias = 0;
51 status_t status;
52
53 for (int i = 0; i < numEntries; i++) {
54 ZipEntry* pNewEntry;
55 int padding = 0;
56
57 pEntry = pZin->getEntryByIndex(i);
58 if (pEntry == NULL) {
59 fprintf(stderr, "ERROR: unable to retrieve entry %d\n", i);
60 return 1;
61 }
62
63 if (pEntry->isCompressed()) {
64 /* copy the entry without padding */
65 //printf("--- %s: orig at %ld len=%ld (compressed)\n",
66 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
67 // (long) pEntry->getUncompressedLen());
68
Raph Levien093d04c2014-07-07 16:00:29 -070069 if (zopfli) {
Dan Willemsenb589ae42015-10-29 21:26:18 +000070 status = pZout->addRecompress(pZin, pEntry, &pNewEntry);
Raph Levien093d04c2014-07-07 16:00:29 -070071 bias += pNewEntry->getCompressedLen() - pEntry->getCompressedLen();
72 } else {
Dan Willemsenb589ae42015-10-29 21:26:18 +000073 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
Raph Levien093d04c2014-07-07 16:00:29 -070074 }
The Android Open Source Project88b60792009-03-03 19:28:42 -080075 } else {
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070076 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
77
The Android Open Source Project88b60792009-03-03 19:28:42 -080078 /*
79 * Copy the entry, adjusting as required. We assume that the
80 * file position in the new file will be equal to the file
81 * position in the original.
82 */
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -070083 off_t newOffset = pEntry->getFileOffset() + bias;
Dmitriy Ivanov13e59652014-07-23 15:27:21 -070084 padding = (alignTo - (newOffset % alignTo)) % alignTo;
The Android Open Source Project88b60792009-03-03 19:28:42 -080085
86 //printf("--- %s: orig at %ld(+%d) len=%ld, adding pad=%d\n",
87 // pEntry->getFileName(), (long) pEntry->getFileOffset(),
88 // bias, (long) pEntry->getUncompressedLen(), padding);
Dan Willemsenb589ae42015-10-29 21:26:18 +000089 status = pZout->add(pZin, pEntry, padding, &pNewEntry);
The Android Open Source Project88b60792009-03-03 19:28:42 -080090 }
91
Elliott Hughesad7d5622018-10-08 11:19:28 -070092 if (status != OK)
The Android Open Source Project88b60792009-03-03 19:28:42 -080093 return 1;
94 bias += padding;
95 //printf(" added '%s' at %ld (pad=%d)\n",
96 // pNewEntry->getFileName(), (long) pNewEntry->getFileOffset(),
97 // padding);
98 }
99
100 return 0;
101}
102
103/*
104 * Process a file. We open the input and output files, failing if the
105 * output file exists and "force" wasn't specified.
106 */
Fabien Sanglardd4f71a92020-10-22 17:58:12 -0700107int process(const char* inFileName, const char* outFileName,
Dan Willemsenb589ae42015-10-29 21:26:18 +0000108 int alignment, bool force, bool zopfli, bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800109{
110 ZipFile zin, zout;
111
112 //printf("PROCESS: align=%d in='%s' out='%s' force=%d\n",
113 // alignment, inFileName, outFileName, force);
114
115 /* this mode isn't supported -- do a trivial check */
116 if (strcmp(inFileName, outFileName) == 0) {
117 fprintf(stderr, "Input and output can't be same file\n");
118 return 1;
119 }
120
121 /* don't overwrite existing unless given permission */
122 if (!force && access(outFileName, F_OK) == 0) {
123 fprintf(stderr, "Output file '%s' exists\n", outFileName);
124 return 1;
125 }
126
Elliott Hughesad7d5622018-10-08 11:19:28 -0700127 if (zin.open(inFileName, ZipFile::kOpenReadOnly) != OK) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800128 fprintf(stderr, "Unable to open '%s' as zip archive\n", inFileName);
129 return 1;
130 }
131 if (zout.open(outFileName,
132 ZipFile::kOpenReadWrite|ZipFile::kOpenCreate|ZipFile::kOpenTruncate)
Elliott Hughesad7d5622018-10-08 11:19:28 -0700133 != OK)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800134 {
Jingwen Owen Ou30321392012-08-17 16:21:11 -0700135 fprintf(stderr, "Unable to open '%s' as zip archive\n", outFileName);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800136 return 1;
137 }
138
Dan Willemsenb589ae42015-10-29 21:26:18 +0000139 int result = copyAndAlign(&zin, &zout, alignment, zopfli, pageAlignSharedLibs);
The Android Open Source Project88b60792009-03-03 19:28:42 -0800140 if (result != 0) {
141 printf("zipalign: failed rewriting '%s' to '%s'\n",
142 inFileName, outFileName);
143 }
144 return result;
145}
146
147/*
148 * Verify the alignment of a zip archive.
149 */
Fabien Sanglardd4f71a92020-10-22 17:58:12 -0700150int verify(const char* fileName, int alignment, bool verbose,
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700151 bool pageAlignSharedLibs)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800152{
153 ZipFile zipFile;
154 bool foundBad = false;
155
156 if (verbose)
157 printf("Verifying alignment of %s (%d)...\n", fileName, alignment);
158
Elliott Hughesad7d5622018-10-08 11:19:28 -0700159 if (zipFile.open(fileName, ZipFile::kOpenReadOnly) != OK) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800160 fprintf(stderr, "Unable to open '%s' for verification\n", fileName);
161 return 1;
162 }
163
164 int numEntries = zipFile.getNumEntries();
165 ZipEntry* pEntry;
166
167 for (int i = 0; i < numEntries; i++) {
168 pEntry = zipFile.getEntryByIndex(i);
169 if (pEntry->isCompressed()) {
170 if (verbose) {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700171 printf("%8jd %s (OK - compressed)\n",
172 (intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800173 }
174 } else {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700175 off_t offset = pEntry->getFileOffset();
Dmitriy Ivanov13e59652014-07-23 15:27:21 -0700176 const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
177 if ((offset % alignTo) != 0) {
The Android Open Source Project88b60792009-03-03 19:28:42 -0800178 if (verbose) {
Chih-Hung Hsieh9b2bf2d2018-08-11 11:33:43 -0700179 printf("%8jd %s (BAD - %jd)\n",
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700180 (intmax_t) offset, pEntry->getFileName(),
Chih-Hung Hsieh9b2bf2d2018-08-11 11:33:43 -0700181 (intmax_t) (offset % alignTo));
The Android Open Source Project88b60792009-03-03 19:28:42 -0800182 }
183 foundBad = true;
184 } else {
185 if (verbose) {
Chih-Hung Hsieh0c0d9282018-08-10 15:14:26 -0700186 printf("%8jd %s (OK)\n",
187 (intmax_t) offset, pEntry->getFileName());
The Android Open Source Project88b60792009-03-03 19:28:42 -0800188 }
189 }
190 }
191 }
192
193 if (verbose)
194 printf("Verification %s\n", foundBad ? "FAILED" : "succesful");
195
196 return foundBad ? 1 : 0;
197}
198
Fabien Sanglardd4f71a92020-10-22 17:58:12 -0700199} // namespace android